FOR - CMD


Overview

The FOR command in Windows Command Prompt (CMD) is a powerful tool used for loop-based command execution. The primary purpose of FOR is to process a set of files or a series of items and apply commands to each in turn. It is especially useful for automating repetitive tasks, processing large batches of files, or setting up complex command sequences in scripts.

Syntax

The FOR command comes in several forms, each suited for different scenarios:

  1. File Processing:

    FOR %variable IN (set) DO command [command-parameters]
    
    • set specifies a file set or range.
  2. Listing of Files (Includes Recursive Option):

    FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]
    
    • /R specifies recursive operation through directories.
  3. Command Output Processing:

    FOR /F ["options"] %variable IN ('command_to_process') DO command [command-parameters]
    
    • /F processes the output of a command.
  4. Literal Text String Processing:

    FOR /L %variable IN (start,step,end) DO command [command-parameters]
    
    • /L specifies that the command should run over a range of numbers.

Variables used in FOR loops are typically represented by a single letter, preceded by a percent sign, e.g., %i.

Options/Flags

  • /R : Run command recursively through subdirectories.
  • /F : Process output of another command or parse file data.
    • "options" can specify how to parse files or commands. Common options include delims=, tokens=, and skip=.
  • /L : Iterate over a range of numbers, helpful for numerical operations and sequences.
  • /D : Process only directories, ignoring files.

Examples

  1. Basic File Iteration:

    FOR %i IN (*.txt) DO @echo %i
    
    • Lists all .txt files in the current directory.
  2. Recursive File Search:

    FOR /R C:\ %i IN (*.docx) DO @echo %i
    
    • Recursively lists all .docx files starting from C:\.
  3. Command Output Processing:

    FOR /F "tokens=*" %i IN ('dir /b') DO @echo %i
    
    • Echoes the names of files and directories in the current directory without additional details.
  4. Numeric Sequence:

    FOR /L %i IN (1,1,5) DO @echo %i
    
    • Prints numbers 1 through 5.

Common Issues

  • Escaping Characters: Among the common pitfalls is not properly escaping special characters in command parameters, leading to unexpected results or errors.
  • Variable Expansion: In batch files, ensure you double the percent signs (%%) to avoid issues with variable interpretation.

Integration

FOR can be integrated with other CMD commands to form more complex scripts. For example, deleting all .tmp files in a directory tree:

FOR /R %i IN (*.tmp) DO del "%i"

This combines FOR with the del command for efficient file management.

  • IF: Provides conditional processing in batch scripts.
  • SET: Used for setting or displaying variable values, which can be combined in loops created with FOR.

For further information, visiting the official Microsoft documentation or resources like SS64 can provide more detailed insights and examples.