R Loop Through Subfolders

Conditionally perform a command on several Directories/Folders.

This video tutorial will walk you through a macro to loop through the subfolders of a folder searching for a specific file. Each part of the Excel VBA code i. 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 enclosed in parentheses (file1., another?log). Wildcards must be used. For loops are a good start to automating your code. However if you want to scale this automation to process more and / or larger files, the R apply family of functions are useful to know about. Apply functions perform a task over and over - on a list, vector, etc.

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

Unlike other variants of the FOR command 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:Workreports*'

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:Workreports*') 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:Workreports*') 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.

R Loop Through Subdirectories

Examples

List every subfolder, below the folder C:Work that has a name starting with 'User':
@Echo Off
CD Work
FOR /D /r %%G in ('User*') DO Echo We found %%~nxG

'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

R Loop Through Subfolders

Through
Copyright © 1999-2021 SS64.com
Some rights reserved