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

Card Puncher Data Processing


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]


  • 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. Every special character must be escaped with a ^. Example: echo Hello ^| find “Hello”
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=


Property file

See DOS - Parse a property file


  • 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


Discover More
Card Puncher Data Processing
Dos - For Statement

The for statement: runs a specified command for each file in a set of files runs a specified command for each directory in a set of directory parses files, string or output of command can create...

Share this page:
Follow us:
Task Runner