Yes, read
for /?, but I thought I would explain a little myself.
Here's your standard version:
Code: Select all
set string=\\DPTEST\smspkgf$\GLB0001
for /f "delims=\ tokens=1" %%a in ("%string%") do (
echo %%a
)
for /f =
for's text-processing mode.
"delims=\ tokens=1" = Options.
delims sets which characters act as delimiters in your text.
tokens sets which values are retrieved from the delimited text. There are two more options,
eol (end of line character, not very important) and
usebackq (only important for writing the path to a text file for processing, as explained later).
%%a = Starting letter, case-sensitive, descending alphabetically, which will contain each value specified in
tokens. For example, if
tokens=2,5-8* and this is
%%c:
%%c = token 2
%%d = token 5
%%e = token 6
%%f = token 7
%%g = token 8
%%h = "token *", the remainder of the string unprocessed (so it includes all remaining delimiters)
in ("%string%") = here you specify what type of output
for /f will get its text to process from. So:
in (fileset) = text file to open and process the text inside, line by line.
in ("string") = string to process.
in ('command') = command to execute, and any text output is processed.
Obviously
fileset cannot contain spaces or special characters in the path to a text file. You also cannot process the content of a string including quotation marks around it (though you could just supply them within your code). To fix this, simply writing the option
usebackq into the options area of
for /f will change the quotation mark processing as follows:
in ("fileset") = text file to open and process the text inside, line by line.
in ('"string"') = string to process.
in (`command`) = command to execute, and any text output is processed.
In both cases,
command can be passed within quotation marks as well ('"command"' or `"command"`), to ease special character handling.
So what our specific example does is, from
%%a...
\\DPTEST\smspkgf$\GLB0001\\token 1\(token 2)\(token 3)..
%%a......_______.._______