DOS - Parsing (File, Command, Variable) - FOR F option

About

parsing in DOS is done via the F option of the FOR command

Syntax

FOR /F ["parsing options"] %variable IN (file-set|string|command) DO command [command-parameters]

where:

  • the option /F means (file|text) processing
  • % and %% indicates that we are in presence of a variable
    • % is used in on-line mode
    • %% is used in batch mode
  • variable is a single letter variable
  • file set file-set is one or more file names (see file set for the definition). Each file is opened, read and processed before going on to the next file in file-set. Processing consists of reading in the file, breaking it up into individual lines of text and then parsing each line into zero or more tokens. If the file path contains a space, you can prepand it with the type command. (ie 'type “path with space\in it.txt”')
  • string and command are text to be parsed
  • command is to parse the output of a command. You do this by making the file-set between the parenthesis a back quoted string. It will be treated as a command line, which is passed to a child CMD.EXE and the output is captured into memory and parsed as if it was a file. <note important>Every special character must be escaped with a ^. Example: echo Hello ^| find “Hello”</note>
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=

Example

Property file

Tokens

  • A token switch example with the third token initialized
@for /F "tokens=3 delims=." %%x in ("MyFirstToken.MySecondToken.MyThirdToken") DO @(
	@echo %%x, %%y, %%z
)
MyThirdToken, %y, %z
  • The first and the third token must be initialized
@for /F "tokens=1,3 delims=." %%x in ("MyFirstToken.MySecondToken.MyThirdToken") DO @(
	@echo %%x, %%y, %%z
)
MyFirstToken, MyThirdToken, %z
  • Initialization of the second token with star
@for /F "tokens=2* delims=." %%x in ("MyFirstToken.MySecondToken.MyThirdToken") DO @(
	@echo %%x, %%y, %%z
)
MySecondToken, MyThirdToken, %z
  • Initialization of the tokens from 1 to 3
@for /F "tokens=1-3 delims=." %%x in ("MyFirstToken.MySecondToken.MyThirdToken.MyFoursteToken") DO @(
	@echo %%x, %%y, %%z, %%a
)
MyFirstToken, MySecondToken, MyThirdToken, %a

Parse the output of a command

Example: get all environment variable name with the help of the set command where:

  • The delimiter is =
  • usebackq is important to allows the use of backquote around the command
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
.......

Powered by ComboStrap