FOR /D

Conditionally perform a command on several Directories/Folders.

Syntax
      FOR /D [/r] %%parameter IN (folder_set) DO command

Key
   folder_set  :  A set of one or more folders enclosed in parentheses (folder1,folder2).
                  Wildcards must 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)

   /r          :  Recurse into subfolders (see notes below)

In all cases for /d will start searching from the current directory.

FOR parameters (%%A – %%Z)

Read the main FOR introduction page for a full description of assigning the replaceable %%parameter.
FOR parameters are used in all variations of the FOR command, it is a good idea to get up to speed with writing a basic FOR command before trying the more complex FOR / D variant.

Unlike other variants of the FOR command, with FOR /D you must include a wildcard (either * or ?) in the 'folder_set' to get consistent results returned. In many cases you can work around this by adding a single character wildcard e.g. if you are looping through multiple folders to find the exact folder January you could instead specify Janu?ry

If any path in the the folder_set includes spaces, then surround the path with double quotes.
If specifying a full path to any of the folders, the path separators must use a backslash, not a forward slash."C:\Work"

The option /d /r is undocumented, but can be a useful combination, while it will recurse through all subfolders the wildcard will only match against Folder/Directory names (not filenames).

Alternatives

An alternative command to list folders and sub folders matching a wildcard is DIR:
C:\> dir /b /s /a:d "C:\Work\reports*"

To loop through each folder programatically, we can wrap that in a FOR /F command:
C:\> for /f "tokens=*" %G in ('dir /b /s /a:d "C:\Work\reports*"') do echo Found %G

or the same thing in a batch file, with the %'s doubled:
for /f "tokens=*" %%G in ('dir /b /s /a:d "C:\Work\reports*"') do echo Found %%G

Period/Full Stop

Although Win32 will not recognise any file or directory name that begins or ends with a '.' (period / full stop) it is possible to include a Full Stop in the middle of a directory name and this can cause issues with FOR /D.

Parameter expansion will treat a Full Stop as a file extension, so for a directory name like "Sample 2.6.4" the output of %%~nG will be truncated to "Sample 2.6" to return the whole folder name use %%G or %%~nxG

FOR does not, by itself, set or clear the Errorlevel.
FOR is an internal command.

Examples

List every subdirectory, below C:\Work\ that has a name starting with "User":

@Echo Off
CD \Work
FOR /D /r %%G in ("User*") DO Echo We found %%~nxG

Recurse through all the directories below C:\demo and if any have the directory name "version 1" rename it to "version 2":

@Echo Off
FOR /D /R %%a in (C:\demo) DO if "%~Na" equ "version 1" ren "%%a" "version 2"
(adapted from this forum answer by foxidrive)

"I knew of one little DO loop that ran for 48 hours, cost $14,000 and did nothing" ~ Balfour and Marwick

Related commands

FOR - Loop commands.
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 PowerShell: ForEach-Object - Loop for each object in the pipeline.
Equivalent bash command (Linux): for - Expand words, and execute commands


 
Copyright © 1999-2024 SS64.com
Some rights reserved