Likewise other programming languages, batch file programming does support looping with the
help of got statements and for statement. Since we have already learnt about the ‘goto’ statement, we are
going to see how the ‘for’ statement works.
Like many other command, the ‘for’ statement do have its own switches for operating in various ways.
The commonly used switches are,
For /D – The ‘/D’ switch along with the ‘for’ command is used for looping through
For /R – The ‘/R’ switch along with the ‘for’ command is used for looping through
Directories and sub directories.
For /L – The ‘/L’ switch along with the ‘for’ command is used for looping through
a range of specified numbers.
For /F – The ‘/F’ switch along with the ‘for’ command is used for looping
through a wide variety of files, command and strings.
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~I – expands %I removing any surrounding quotes (“)
%~fI – expands %I to a fully qualified path name
%~dI – expands %I to a drive letter only
%~pI – expands %I to a path only
%~nI – expands %I to a file name only
%~xI – expands %I to a file extension only
%~sI – expanded path contains short names only
%~aI – expands %I to file attributes of file
%~tI – expands %I to date/time of file
%~zI – expands %I to size of file
%~$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
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
Here I am going to cover all kinds of usage of the ‘for’ command along with some code snippets,
FOR /D %v IN (*.*) DO dir/s “%v”
As said earlier, that the ‘/D’ switch along with the ‘for’ command is used for looping through the
directories and sub-directories. The above given command is used for displaying all the directories and
Note: When you execute this command right from the command prompt by copying it and pasting it will
work, but when you create a batch file using this code, won’t work, because when you are using it in a
batch file, you are supposed to use %% preceding the variable name, in this case the following code will
work if you try to execute as a batch,
FOR /D %%v IN (*.*) DO dir/s “%%v”
The ‘/R’ switch when used with the ‘for’ command is used for looping through Directories and sub
FOR /R C:\Windows\Prefetch %v IN (*.pf) DO del %v
The above piece of code is used for deleting prefetch files from the location C:\windows\prefetch, which
are considered to be unnecessary and which hogs up the memory, hence I am going to use the above
command for deleting the prefetch files that has the .pf extension, also I have enclosed the screenshot
captured while I was executing this statement.
The ‘/L’ switch when used with the ‘for’ statement is used for looping through a wide variety of specified
numbers. In the below example, I have enclosed a snippet that I used for finding open port and if an open
port is found, then it will telnet to it and establish a remote connection, but the user has to supply the IP
address or the hostname as parameter to this program, only then it becomes effective.
for /L %%v in (1,1,20) do telnet %1 %%v
If you notice the set (1,1,20) that contains 1,1,20, where the ‘1’ in the front denotes the initial
value for the loop, the second ‘1’ denotes the increment value or the step value, since it is stated as ‘1’
over here, the loop will be incremented by one and finally the ‘20’ denotes the end value, indicating that
the loop was supposed to be terminated when the count reaches 20.
The following piece of code will help you to better understand how it works,
FOR /L %G IN (20,-2,0) DO ECHO %G
Here is the output for this statement,
If you notice the set, it has some negative values too which in turn describes that we can use negative
integers too while looping using the ‘for’ statement.