by Barry Dysert
(last updated August 11, 2014)
The FOR statement is a command that allows you to "loop" or repeat commands multiple times. It is very similar to the FOR statement provided in several programming languages. The FOR statement is often used in batch files, but it can also be used directly on the command line.
As a reminder, the general form of the FOR loop is:
FOR %variable IN (set) DO command [command-parameters]
Where 'variable' is a single letter, '(set)' specifies a set of one or more files, 'command' is the command-prompt command to be executed for each file in the set, and 'command-parameters' are optional parameters to be passed to 'command'.
So let's say you have been given several COM .dll files that you need to register within Windows' Registry. You could put them all in one folder and can then run the following command from within that folder:
C:\DLLs> FOR %i IN (*.dll) DO regsvr32 /s %i
What this does, for each .dll file it finds in the current directory, is to run the regsvr32 program on it. (The "/s" switch simply tells regsvr32 to do its work silently.) The variable used ("%i") is case sensitive, and it assumes the value of each .dll file in turn. The name of the .dll file is then passed to the regsvr32 program using the same "%i" variable.
A variant of the general FOR loop uses the /L switch and has this general form:
FOR /L %variable IN (start,step,end) DO command [command-parameters]
This executes a loop where %variable is first set to 'start' and is incremented by 'step' until it hits the value 'end'. (This usage should be very familiar to programmers.) So let's say you need to display the list of numbers from 1 to 5. You could do it this way:
C:\> FOR /L %i IN (1,1,5) DO @echo %i
Another variant of the FOR loop uses the /D switch and has this general form:
FOR /D %variable IN (set) DO command [command-parameters]
With this variant, if 'set' contains wildcards, then the command works on directory names instead of file names. This would be useful, for example, if you wanted to copy a set of files from a directory tree and put all the files in one directory.
For example, I have a directory called \Temp. It contains subdirectories called \Temp\Temp2 and \Temp\Barry. The \Temp\Barry and the \Temp\Temp2 directories contain files I want to copy. If I wanted to copy all of the files (without the directory structure) in the \Temp tree to a directory called \Files, I could do it like this:
C:\Temp> FOR /D %i IN (*) DO Copy %i\*.* \Files
I have left to the end what I think is the most esoteric form of the FOR loop. It seems that using the /F switch will allow you to do almost anything—if you can only figure out how to use it. The general form is:
FOR /F "options" %variable IN (`command`) DO command [command-parameters]
(Sorry it doesn't all fit on one line.) There are several 'options you can use, and the first 'command' (the one surrounded by the backwards apostrophes) can be any command-prompt command. So here's one example of how to use this:
FOR /F "Usebackq Delims==" %i IN (`dir/b c:\temp\a*.tmp`) DO @echo c:\temp\%i | Findstr /f:/ "a b c"
(Sorry it doesn't all fit on one line.) This command performs a DIR/B command on C:\Temp\A*.tmp. For each file it finds, it echoes the file name and pipes it to the Findstr command, which looks for all lines containing an "a", "b", or "c". A pretty odd thing to do, granted, but at least it shows how you can use /F. I suggest you do FOR /? to get a complete list of what all the options are, and then you're basically bound only by your imagination on figuring out how to use it.
You can also use the FOR command within a batch file. The only thing that's different is that you double the %-signs, whereas at the command line you only use one %-sign. For example, if the second example above were to be placed in a batch file, it would look like this:
FOR /L %%i IN (1,1,5) DO @echo %%i
This tip (13196) applies to Windows 7 and 8.
One of the powerful capabilities built into Windows is the ability to create and use batch files to perform a wide variety of ...Discover More
PowerShell is found on all modern Windows computers. You might think of it as the next step up from the command line .bat ...Discover More
This tip is part of a series that talks about Windows batch files. It introduces a few more commands and special characters ...Discover More
FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."