tokens & delims help

  • Thread starter Thread starter William Hymen
  • Start date Start date
W

William Hymen

Hi all,

I have a batch file which will
input a full path name with:
set /p Folder_Path="Enter the full path and folder"

The user will then paste in something like:

d:\root_folder\second\third\fourth\final -or-
d:\root_folder\second\third\final
d:\root_folder\second\final

I would like to parse the foldername "final"
and place it into variable TARGET, and I'm not sure
how to use tokens and delims to get the very
last string after the final "/"

Thanks in advance-

Bill
 
Hi all,

I have a batch file which will
input a full path name with:
set /p Folder_Path="Enter the full path and folder"

The user will then paste in something like:

d:\root_folder\second\third\fourth\final -or-
d:\root_folder\second\third\final
d:\root_folder\second\final

I would like to parse the foldername "final"
and place it into variable TARGET, and I'm not sure
how to use tokens and delims to get the very
last string after the final "/"

This demo shows how to write a loop to grab the final
token in the path into variable TARGET:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SETLOCAL
SET Folder_Path=d:\root_folder\second\third\fourth\final
SET Target=%Folder_Path%
:LOOP
FOR /f "tokens=1* delims=\" %%A IN ("%Target%") DO (
IF NOT "%%B"=="" SET Target=%%B&GOTO LOOP
)
ECHO. Folder_Path=%Folder_Path%
ECHO. Target=%Target%

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

For help with the FOR command, use the /? switch:
for /?
 
set /p Folder_Path="Enter the full path and folder"
FOR %%I IN ( "%Folder_Path%" ) DO SET Target=%%~nxI
ECHO %Target%

If Folder_Path can contain long file name with spaces, then the "s are
necessary for this to work (either you insert them, or user has to enter
it).

--
//David
IIS
http://blogs.msdn.com/David.Wang
This posting is provided "AS IS" with no warranties, and confers no rights.
//
Hi all,

I have a batch file which will
input a full path name with:
set /p Folder_Path="Enter the full path and folder"

The user will then paste in something like:

d:\root_folder\second\third\fourth\final -or-
d:\root_folder\second\third\final
d:\root_folder\second\final

I would like to parse the foldername "final"
and place it into variable TARGET, and I'm not sure
how to use tokens and delims to get the very
last string after the final "/"

Thanks in advance-

Bill
 
See tip 0494 » How to parse a batch parameter.
in the 'Tips & Tricks' at http://www.jsifaq.com

When you invoke a batch file with a parameter (%1), you are able to parse it to extract meaningfull information (if Command Extension are enabled). See the following examples:

Parameter D e s c r i p t i o n
%1 The normal parameter.
%~f1 expands %1 to a fully qualified path name.
%~d1 expands %1 to a drive letter only.
%~p1 expands %1 to a path only.
%~n1 expands %1 to a file name only (prefix)
%~x1 expands %1 to a file extension only.
%~s1 changes the meaning of n and x options to reference the short name.
%~z1 Returns the size of the file, if %1 is a file.
%~a1 Returns the attributes of the %1 object.
%~t1 Returns the date and time that a file was modified, if %1 is a file.


You can use some of these modifiers in combination:

Parameter D e s c r i p t i o n
%~dp1 expands %1 to a drive letter and path only.
%~nx1 expands %1 to a file name and extension only.
 
From the "for /?" inline help -

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
 
Back
Top