Dos - For Statement

1 - About

The for statement:

To get more information on this statement, just type in a command dos:

help for

3 - Variable

3.1 - Mode

The variable element initialization differs from one interaction mode to the other

  • %variable is used in on-line mode
  • %%variable is used in (batch|script) mode

3.2 - Syntax Enhancement

To understand the syntax enhancement, you must understand the FOR loop constructs. This why they are at the end of this page: see variable enhancement

4 - Type For

4.1 - File Set

4.1.1 - Syntax

FOR [/R path] (% of %%)variable IN (set) DO command [command-parameters]

where:

  • the /R option indicates if you want to recursively walk the directory tree from the path parameter (default: current directory)
  • % 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
  • set is a file set data definition (not the set function of variable). You can use the ster wildcard and use a set definiton. To get all doc and txt files, you will define (set) as:
(*.doc *.txt )
  • command-parameters: specifies parameters or switches for the specified command.

4.1.2 - Example

4.1.2.1 - List all files in one directory
:: In the command line
for %v in (*.bat) do echo %v
:: In a DOS Script
for %%v in (*.bat) do echo %%v

where:

  • v is the variable
  • *.bat is the set of all file with a bat extions file

Example of output:

start.bat
Hello.bat
4.1.2.2 - List all files in the directory tree

Current directory = C:\Users\gerard

:: In the command line
for /R %v in (*.bat) do echo %v
:: In a DOS Script
for /R %%v in (*.bat) do echo %%v
:: Is the same that
for /R C:\Users\gerard %%v in (*.bat) do echo %%v
C:\Users\gerard\AppData\Local\Temp\cleanup_bootstrap.bat
C:\Users\gerard\AppData\Local\Temp\env.bat
C:\Users\gerard\AppData\Local\Temp\startup.bat
C:\Users\gerard\AppData\Local\Temp\update_acls1.bat
C:\Users\gerard\AppData\Local\Temp\update_acls2.bat
..........

4.2 - Directory Set

4.2.1 - Syntax

FOR /D (%!!|%%)variable IN (set) DO command [command-parameters]

where:

  • % 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
  • set is a directory set data definition (not the set function of variable). You can use the star wildcard and use a set definition. To get all directory which start with Doc and , you will define (set) as:
(App* Pic*)
  • command-parameters: specifies parameters or switches for the specified command.

4.2.2 - Example

:: In the command line
FOR /D %v IN (App* Pic*) DO echo %v
:: In a DOS Script
FOR /D %%v IN (App* Pic*) DO echo %%v

where:

  • v is the variable
  • (App* Pic*) is the set of directory that start with App and Pic

Example of output from the User Home:

AppData
Pictures

4.3 - Sequence of number

4.3.1 - Syntax

FOR /L (%!!|%%)variable IN (start,step,end) DO command [command-parameters]

where:

  • /L indicate a number for loop
  • start indicates the first number of the sequence
  • end indicates the last number of the sequence
  • step indicate the number of number to jump

4.3.2 - Example

The set is a sequence of numbers from start to end, by step amount.

FOR /L %v IN (3,3,15) DO echo %v

Output:

3
6
9
12
15

4.4 - (File|Text|Command Output) Parsing

4.4.1 - 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. 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=

4.4.2 - Example

4.4.2.1 - 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
4.4.2.2 - Parse the output of a 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
.......
4.4.2.3 - Parse a property file
# Comments
myHostName=gerardnico.local
myPort=1521

Dos Script to list the content

:: The lines beginning with # will be skipped
FOR /F "tokens=1,2* delims== eol=# " %%i in ('type "path to\file.properties"') do (
	@echo i = %%i 
	@echo j = %%j
	)
i = myHostName
j = gerardnico.local
i = myPort
j = 1521

Conditional processing with a if statement:

FOR /F "tokens=1,2* delims== eol=# " %%i in ('type "path to\file.properties"') do (
	if "%%i" == "myHostName" @echo    - My Host Name is  %%j 
	if "%%i" == "myPort" @echo    - My Port is  %%j 
	)
   - My Host Name is  gerardnico.local
   - My Port is  1521

5 - Variable Enhancement

5.1 - Syntax

%~mI

where:

  • %~
  • m is a modifier (They are not case sensitive)
  • I is valid FOR variable

Picking upper case variable names like %I makes it more readable and avoids confusion with the modifiers, which are not case sensitive.

5.2 - List of modifiers

List of syntax enhancement with example: In the below examples %I can be replaced by other valid values.

5.2.1 - No Modifiers: removing any surrounding quotes

%~I expands %I removing any surrounding quotes (“)

5.2.2 - f: fully qualified path name

%~fI expands %I to a fully qualified path name

for %I in (.) do @echo %~fI
c:\Users\gerard

5.2.3 - d: Drive letter only

%~dI expands %I to a drive letter only

for %I in (.) do @echo %~dI
c:

5.2.4 - p: Path only

%~pI - expands %I to a path only

for %I in (.) do @echo %~pI
\Users\

5.2.5 - n: file name only

%~nI expands %I to a file name only

for %I in (.) do @echo %~nI
gerard

5.2.6 - x: file extension only

%~xI expands %I to a file extension only

for %I in (temp.xml) do @echo %~xI
.xml

5.2.7 - s: short names only

%~sI expanded path contains short names only. Example from C:\Program Files

for %I in (.) do @echo %~sI
C:\PROGRA~1

5.2.8 - a: file attributes of file

%~aI expands %I to file attributes of file

for %I in (.) do @echo %~aI
d--------

5.2.9 - t: date/time of file

%~tI expands %I to date/time of file

for %I in (.) do @echo %~tI
2013-10-08 12:01

5.2.10 - z: size of file

%~zI expands %I to size of file

for %I in (.) do @echo %~tI
28672

5.2.11 - $PATH: Search file in directories

%~$PATH:I searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string. Example: Where can I find SQLPLUS.exe

for %I in (sqlplus.exe) do @echo %~$PATH:I
C:\app\gerard\product\12.1.0\dbhome_1\BIN\sqlplus.exe

PATH can be replaced by other valid values.

5.2.12 - Modifiers Combination

The modifiers can be combined to get compound results:

  • %~dpI - expands %I to a drive letter and path only
  • %~nxI - expands %I to a file name and extension only
  • %~fsI - expands %I to a full path name with short names only
  • %~dp$PATH:I - searches the directories listed in the PATH environment variable for %I and expands to the drive letter and path of the first one found.
  • %~ftzaI - expands %I to a DIR like output line

6 - Support

6.1 - %% was unexpected at this time

See variable

lang/dos/for.txt · Last modified: 2018/08/27 18:26 by gerardnico