Home NT Commands
NT Syntax

FOR

Conditionally perform a command on several files.

Syntax
      FOR %%parameter IN (set) DO command

Key
   command     : The command to carry out, including any 
                 command-line parameters.

   %%parameter : A replaceable parameter:
                 e.g. in a batch file use %%G 
                      (on the command line %G)

Examples

Copy a single file
FOR %%G IN (MyFile.txt) DO copy %%G d:\backups\

Copy a list of several files
FOR %%G IN (Myfile.txt SecondFile.txt) DO copy %%G d:\backups\

Copy a single file
FOR %%G IN (c:\program^ files\MyFile.txt) DO copy %%G d:\backups\

In the line above the source folder has spaces that need to be escaped with ^

Although wildcards can be used an alternative method of processing files is to process the output of the command 'DIR /b'

This can be useful when you want to use some of the other DIR options like sorting.

"Darkness reigns at the foot of the lighthouse" - Japanese Proverb

Related Commands:

FOR - Loop commands

FOR /R - Loop through files (recurse subfolders)
FOR /D
- Loop through several folders
FOR /L - Loop through a range of numbers
FOR /F - Loop through items in a text file
FOR /F - Loop through the output of a command

FORFILES - Batch process multiple files
GOTO - Direct a batch program to jump to a labelled line
IF - Conditionally perform a command

Equivalent Linux BASH commands:

cut - Divide a file into several columns
for
- Expand words, and execute commands
eval - Evaluate several commands/arguments
until - Execute commands (until error)



Simon Sheppard
SS64.com