Home > Commands A-M > Batch F

FOR /D


Description | Syntax | Parameters | Switches | Related | Notes | Examples | Errorlevels | Availability

Conditionally perform a command on several folders.


Syntax

FOR /D /?

To use FOR in a batch program:

FOR /D [/r] %%variable IN ('command_to_process) DO command [command-parameters]

To use FOR from the command prompt:

FOR /D [/r] %variable IN ('command_to_process) DO command [command-parameters]


Parameters
command (NT4)
The command to carry out, including any command-line parameters.
command-parameters (NT4)
Specifies any parameters or switches that you want to use with the specified command (if the specified command uses any parameters or switches).
(set) (NT4)
A set of one or more folders. Wildcards may be used.
%variable or %%variable (NT4)
Represents a replaceable variable. The FOR command replaces %%variable (or %variable) with each text string in the specified set until the command (specified in the command parameter) processes all the folders. Use %%variable to carry out the FOR command within a batch program. Use %variable to carry out FOR from the command prompt. (Do NOT use %0 through %9 to avoid confusion with the command-line arguments passed to the batch file).

Switches
/? (NTXP)
Display help.
/r (NT4)
Recurse into subfolders.

Related

FOR - Loop through a set of files in one folder.
FOR /R - Loop through files (recurse subfolders).
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.
case - Conditionally perform a command.
eval - Evaluate several COMMANDS/arguments.
if - Conditionally perform a command.
gawk - Find and Replace text within file(s).
m4 - Macro processor.
until - Execute commands (until error).
while - Execute commands.


Notes

none.


Examples

List separately each subfolder that is one level below the current folder:

    FOR /D %%G IN (*.*) DO ECHO ***Listing contents of %%G*** & dir/s "%%G"

Errorlevels

none.


Availability
Internal
DOS
none
Windows
none
Windows NT
NT4 NT2000 NTXP NT2003