Posted On 2005-11-1 by FortyPoundHead
Keywords: Command Reference
Tags: Windows Commandline Windows
Views: 1525


Conditionally perform a command several times.


      FOR %%parameter IN (set) DO command

syntax-FOR-Files-Rooted at Path  

      FOR /R [[drive:]path] %%parameter IN (set) DO command


      FOR /D %%parameter IN (folder_set) DO command

syntax-FOR-List of numbers  

      FOR /L %%parameter IN (start,step,end) DO command

syntax-FOR-File contents  

      FOR /F ["options"] %%parameter IN (filenameset) DO command

      FOR /F ["options"] %%parameter IN ("Text string to process") DO command

syntax-FOR-Command Results

      FOR /F ["options"] %%parameter IN ('command to process') DO command

The operation of the FOR command can be summarised as...

Take a set of data

Make a FOR Parameter %%G equal to some part of that data

Perform a command (optionally using the parameter as part of the command).

Repeat for each item of data

If you are using the FOR command at the command line rather than in a batch program, specify %parameter instead of %%parameter.

FOR Parameters

The FOR command supports implicit parameter definitions.  The first parameter always has to be defined explicitly and this must be a single character.

e.g. FOR %%G IN ...

In each iteration of a FOR loop, the IN ( ....) clause is evaluated and %%G set to a different value.  If this results in a single value then %%G is set = to that value and the command is performed.

If this results in a multiple values then extra parameters are implicitly defined to hold each. These are automatically assigned in alphabetical order %%H %%I %%J ...

For example

FOR /F %%G IN ("This is a long sentence") DO @echo %%G %%H %%J

will result in the output

This is long

You can of course pick any letter of the alphabet other than %%G.

%%G is a good choice because it does not conflict with any of the pathname format letters (a, d, f, n, p, s, t, x) and provides the longest run of non-conflicting letters for use as implicit parameters.

G > H > I > J > K > L > M

Other Environment variables

Environment variables within a FOR loop are expanded at the beginning of the loop and won't change until AFTER the end of the DO section. So in the following snippet %count% will not display the expected result:

SET count=1

FOR /f "tokens=*" %%G IN ('dir /b') DO (

   echo %count%:%%G

   set /a count+=1)

To force the variable to be evaluated during each iteration, use the CALL :subroutine mechanism to take the evaluation outside of the FOR loop.

SET count=1

FOR /f "tokens=*" %%G IN ('dir /b') DO (call :s_do_sums %%G)

GOTO :eof


  echo %%count%%:%1

  set /a count+=1

GOTO :eof

Nested FOR commands

FOR commands can be nested FOR %%G... DO (for %%U... do ...)

when nesting commands choose a different letter for each part. you can then refer to both parameters in the final DO command.

If Command Extensions are disabled, the FOR command will only support the basic syntax with no enhanced variables:

FOR %%parameter IN (set) DO command [command-parameters]

About the Author

FortyPoundHead has posted a total of 1974 articles.


Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Recent Forum Posts

Malicious IP Checker Companion Tool
dwirch posted on August 12, 2017 at about 20:24 in Site News

Job Spammer: Steve Adams
dwirch posted on August 8, 2017 at about 7:44 in Spammers

New security implemented
dwirch posted on August 8, 2017 at about 6:43 in Site News

New security implemented
dwirch posted on July 23, 2017 at about 6:58 in Site News

Fold Code Manager into main KB?
VB6Boy posted on July 22, 2017 at about 14:42 in Site News

Fold Code Manager into main KB?
dwirch posted on July 22, 2017 at about 14:41 in Site News