parsing in DOS is done via the F option of the FOR command
FOR /F ["parsing options"] %variable IN (file-set|string|command) DO command [command-parameters]
where:
parsing Options | Description | Default |
---|---|---|
eol=c | End of line or comment character (just one) | semi-colon ; |
skip=n | Specifies the number of lines to skip at the beginning of the file. | O (Blank lines are skipped) |
delims=xxx | Specifies a delimiter set. This replaces the default delimiter set of space and tab. | one space |
tokens=x,y,m-n | Specifies which (tokens|variable) from each line that must be initialized. The m-n form is a range, specifying the mth through the nth tokens. If the last character is an asterisk, then an additional variable is allocated and receives the remaining text on the line after the last token parsed. | 1 |
usebackq | Allows the use of double quotes to quote file names in file-set or to quote the string | Not used |
By default, /F passes the first blank separated token from each line of each file.
To suppress the default, just set the parsing option without value. Ex: delims=
@for /F "tokens=3 delims=." %%x in ("MyFirstToken.MySecondToken.MyThirdToken") DO @(
@echo %%x, %%y, %%z
)
MyThirdToken, %y, %z
@for /F "tokens=1,3 delims=." %%x in ("MyFirstToken.MySecondToken.MyThirdToken") DO @(
@echo %%x, %%y, %%z
)
MyFirstToken, MyThirdToken, %z
@for /F "tokens=2* delims=." %%x in ("MyFirstToken.MySecondToken.MyThirdToken") DO @(
@echo %%x, %%y, %%z
)
MySecondToken, MyThirdToken, %z
@for /F "tokens=1-3 delims=." %%x in ("MyFirstToken.MySecondToken.MyThirdToken.MyFoursteToken") DO @(
@echo %%x, %%y, %%z, %%a
)
MyFirstToken, MySecondToken, MyThirdToken, %a
Example: get all environment variable name with the help of the set command where:
FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i
The command must be between back quoted string
ALLUSERSPROFILE
ANT_HOME
APPDATA
ARBORPATH
BIOFFICETARGETDIR
BIOFFICETARGETDITPPT
CATALINA_HOME
CLASSPATH
CommonProgramFiles
CommonProgramFiles(x86)
CommonProgramW6432
COMPUTERNAME
ComSpec
DERBY_HOME
ENDECA_HOME
EPM_HOME
EPM_ORACLE_HOME
.......