The for statement:
To get more information on this statement, just type in a command dos:
help for
The variable element initialization differs from one interaction mode to the other
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
FOR [/R path] (% of %%)variable IN (set) DO command [command-parameters]
where:
(*.doc *.txt )
:: In the command line
for %v in (*.bat) do echo %v
:: In a DOS Script
for %%v in (*.bat) do echo %%v
where:
Example of output:
start.bat
Hello.bat
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
..........
FOR /D (%|%%)variable IN (set) DO command [command-parameters]
where:
(App* Pic*)
:: 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:
Example of output from the User Home:
AppData
Pictures
FOR /L (%|%%)variable IN (start,step,end) DO command [command-parameters]
where:
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
for parsing with the /F switch, see DOS - Parsing (File, Command, Variable) - FOR F option
%~mI
where:
Picking upper case variable names like %I makes it more readable and avoids confusion with the modifiers, which are not case sensitive.
List of syntax enhancement with example:
In the below examples %I can be replaced by other valid values.
%~I expands %I removing any surrounding quotes (“)
%~fI expands %I to a fully qualified path name
for %I in (.) do @echo %~fI
c:\Users\gerard
%~dI expands %I to a drive letter only
for %I in (.) do @echo %~dI
c:
%~pI - expands %I to a path only
for %I in (.) do @echo %~pI
\Users\
%~nI expands %I to a file name only
for %I in (.) do @echo %~nI
gerard
%~xI expands %I to a file extension only
for %I in (temp.xml) do @echo %~xI
.xml
%~sI expanded path contains short names only. Example from C:\Program Files
for %I in (.) do @echo %~sI
C:\PROGRA~1
%~aI expands %I to file attributes of file
for %I in (.) do @echo %~aI
d--------
%~tI expands %I to date/time of file
for %I in (.) do @echo %~tI
2013-10-08 12:01
%~zI expands %I to size of file
for %I in (.) do @echo %~tI
28672
%~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.
The modifiers can be combined to get compound results:
See variable