Home NT Commands
NT Syntax

FOR /R

Loop through files (Recurse subfolders)

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

key
   drive:path  : The folder tree where the files are located.

   set         : A set of one or more files. Wildcards may be used.
                 If (set) is a period character (.) then FOR will
                 loop through every folder.

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

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

This command walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file.

If the [drive:]path are not specified they will default to the current drive:path.

For example:
To delete every .bak file in every subfolder starting at C:\temp

FOR /R C:\temp\ %%G IN (*.bak) DO del %%G

Bug Alert
Under NT 4 you will have to use an unquoted shortname (unless you are iterating the current folder.)

Related Commands:

FOR - Loop commands

FOR - Loop through a set of files in one folder
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
if - Conditionally perform a command
m4 - Macro processor
until - Execute commands (until error)
while - Execute commands



Simon Sheppard
SS64.com