How to List All Subdirectories of a Folder in Batch Script
A common task in automation is to get a list of all subdirectories within a specific folder. You might need this list to perform a cleanup operation on each subfolder, to generate a report on a directory structure, or to search for a specific folder name. The primary command-line tool for this in Windows is the DIR command, which has powerful filtering capabilities.
This guide will teach you how to use the DIR command to list immediate subdirectories, how to get a clean, script-friendly list, and how to extend the command to list all folders recursively through an entire directory tree. You will also learn how to capture and use this list in a FOR loop to automate actions on each folder.
The Core Command: DIR /AD
The DIR command is used to display a list of files and subdirectories in a directory. To filter this list to show only directories, you use the /A (Attributes) switch with the D (Directory) attribute.
C:\Windows> DIR /AD
This command produces a standard DIR listing, including header and summary information, but it correctly excludes all files.
Volume in drive C is Windows
...
Directory of C:\Windows
07/26/2022 10:00 AM <DIR> addins
04/15/2023 05:45 AM <DIR> appcompat
...
09/18/2023 11:30 AM <DIR> SysWOW64
... File(s) ...
... Dir(s) ... bytes free
Getting a Clean, Script-Friendly List (/B)
The standard DIR output is verbose and difficult to use in a script. For automation, you usually want a clean list containing only the names of the directories. This is achieved with the /B (Bare format) switch.
C:\Windows> DIR /AD /B
The output of this command is clean, simple, and perfect for a script to read.
addins
appcompat
...
SysWOW64
Listing Subdirectories Recursively (/S)
To get a list of all subdirectories in a folder and all of its subfolders (an entire directory tree), you add the /S (Subdirectories/Recursive) switch.
This command will list every single subdirectory within C:\Windows.
C:\Windows> DIR /AD /S /B
The /S switch causes DIR to print the full path for each subdirectory found.
C:\Windows\addins
C:\Windows\appcompat
C:\Windows\appcompat\appraiser
C:\Windows\appcompat\Programs
...
C:\Windows\SysWOW64\drivers
The Scripting Method: Processing the List with FOR
Simply displaying the list is not automation. To perform an action on each directory, you need to capture this list within a loop.
For Immediate Subdirectories (FOR /D)
The FOR /D command is specifically designed to loop through the subdirectories in a given path. This is the simplest and cleanest method for non-recursive loops.
@ECHO OFF
ECHO --- Subdirectories in the current folder ---
FOR /D %%D IN (*) DO (
ECHO Found directory: "%%D"
)
For Recursive Subdirectories (FOR /F with DIR)
To process a recursive list, you must use a FOR /F loop to capture the output of the DIR /S command we saw earlier.
@ECHO OFF
ECHO --- All subdirectories in C:\Windows ---
FOR /F "delims=" %%D IN ('DIR "C:\Windows" /AD /S /B') DO (
ECHO Found directory: "%%D"
)
Common Pitfalls and How to Solve Them
Problem: The List Includes Files
This is the most common mistake. If you forget the /AD switch, DIR /B will list everything, and your script will incorrectly try to process files as if they were folders.
Solution: Always use the /AD switch when your goal is to list only directories.
Problem: Hidden Folders are Not Listed
By default, DIR does not show hidden files or directories. If you need to process folders like .git or other application data folders, you must explicitly tell DIR to include them.
Solution: Add the H (Hidden) attribute to the /A switch. The correct switch becomes /ADH.
REM This command lists all directories, including hidden ones.
DIR /ADH /B
Problem: Handling Paths with Spaces
If the starting path for your search contains spaces, you must enclose it in double quotes.
Solution: This is a universal best practice. Always quote your paths.
REM This is the correct, safe syntax.
FOR /F "delims=" %%D IN ('DIR "C:\Program Files" /AD /S /B') DO ( ... )
Practical Example: A Subdirectory Size Report
This script uses the FOR /D loop to iterate through all the immediate subdirectories of a given path and calculates the total size of each one. This is a highly practical use for getting a directory list.
@ECHO OFF
SETLOCAL
SET "SCAN_ROOT=C:\Users\Admin"
ECHO --- Subdirectory Size Report for "%SCAN_ROOT%" ---
ECHO This may take a while...
ECHO.
REM The /D switch loops through directories in the specified path.
FOR /D %%D IN ("%SCAN_ROOT%\*") DO (
CALL :GetFolderSize "%%D"
)
GOTO :EOF
:GetFolderSize
SETLOCAL ENABLEDELAYEDEXPANSION
SET "folder=%~1"
SET "size=0"
REM For each folder, recursively find all files and sum their sizes.
FOR /F "delims=" %%F IN ('DIR "%folder%" /S /A-D /B 2^>NUL') DO (
SET /A "size+=%%~zF"
)
ECHO Size: !size! bytes - Folder: "%folder%"
ENDLOCAL
GOTO :EOF
Conclusion
The DIR command is the definitive tool for listing subdirectories in a batch script, offering precise control over the output.
For reliable directory listing:
- Use
DIR /AD /Bto get a clean, script-friendly list of immediate subdirectories. - Add the
/Sswitch for a recursive list of an entire directory tree. - Add the
Hattribute (/ADH) to include hidden folders. - Use
FOR /Dto loop through immediate subdirectories andFOR /Fwith DIR` to loop recursively.
By mastering these DIR switches and FOR loop patterns, you can write powerful scripts that can navigate and manage any directory structure.