FOR

Conditionally perform a command on several files, or a list of folders.

Syntax
      FOR %%parameter IN (set) DO command

Key
   set         : A set of one or more files or folders, separated by any standard delimiter.
                 enclosed in parentheses (file1,file2). Wildcards can be used.

   command     : The command to carry out, including any parameters.
                 This can be a single command, or if you enclose it in (brackets),
                 several commands, one per line.

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

The FOR command is mostly used to process files, but you can also process a fixed folder name or a text string:
FOR %%G IN ("Hello World") DO Echo %%G

If the (set) includes a wildcard, then FOR will only match against files, not folders.

FOR will not always read the file system, if you use Parameter Extensions to gather File/folder attributes, Date/time or File-size, those will be read from the filesystem. Other attributes like ~n ~p ~x (for File Name/path/extension ) will read from the filesystem if the file/folder exists, otherwise it will use a regex to return a value from the provided set. ~d (Drive letter) is extracted from the set if provided, otherwise it defaults to the current drive.

Although wildcards can be used to select files here, an alternative method of processing files is to have FOR /F process the output of the command 'DIR /b' This can be useful when you want to specify DIR options like sorting.

FOR parameters (%%A – %%Z)

Read the main FOR introduction page for a full description of assigning the replaceable %%parameter.

Errorlevels

FOR does not, by itself, set or clear an Errorlevel, leaving that to the command being called.
One exception is using a wildcard, if the wildcard does not match any files, then FOR will return %ERRORLEVEL% = 5

FOR is an internal command.

Examples

If you are using the FOR command at the command line rather than in a batch program, use just one percent sign: (%G instead of %%G).

Echo the days of the week:

FOR %%G IN (Monday Tuesday Wednesday Thursday Friday) DO Echo %%G

Echo the days of the week putting them into a sentence:

FOR %%G IN (Monday Tuesday Wednesday Thursday Friday) DO Echo Every %%G is a working day!

Notice that in these examples you could have a set of files or folders matching the days of the week, but FOR is not going to check that, it will blindly process them as text strings.

Process a text string which may or may not match a real folder/directory on the file system:

FOR %%G IN ("C:\demo\idontexist\") DO Echo %%nG\

Copy a single file:

FOR %%G IN (MyFile.txt) DO copy %%G d:\backups\

Copy a list of several files:

FOR %%G IN (file1.txt File2.txt) DO copy %%G d:\backups\
or
FOR %%G IN ("C:\demo files\file1.txt" "C:\demo files\File2.txt") DO copy %%G d:\backups\

"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 bash command (Linux): for - Expand words, and execute commands or read (in a loop) - Read a line from standard input.


 
Copyright © 1999-2025 windevcluster.com
Some rights reserved