How to Count the Number of Subdirectories in a Directory
When managing complex folder structures or performing automated cleanups, you often need to know how many subdirectories exist within a specific path. You might want to verify that a deployment script created the correct number of folders or check if a user's profile has too many subdirectories. While there is no single "count folders" command, you can easily get this number by filtering the output of the DIR command.
This guide will teach you the most reliable method for counting subdirectories using a DIR command piped to FIND. You will learn how to capture this count into a variable and how to extend the command to perform a recursive count of all folders in an entire directory tree.
The Core Method: Combining DIR and FIND
The most effective way to count directories is to first generate a list of them and then count the number of lines in that list.
DIR /AD /B: This command is the key to getting a clean list of just the subdirectories./AD: This attribute switch is crucial. It tellsDIRto list only items that are Directories./B: This switch provides a "bare" format, listing only the name of each subdirectory, one per line.
FIND /C /V "": This is the standard trick for counting lines./C: This switch tellsFINDto Count the matching lines./V "": This finds lines that are not (/V) empty (""), which effectively counts every line of input.
When you pipe (|) these two commands together, you get a direct count of the subdirectories.
DIR /AD /B "Path" | FIND /C /V ""
Basic Example: Counting Subdirectories
Let's run this command in a folder with a mix of files and subdirectories.
For example in this directory (C:\MyProject\) that contains:
- config.xml
- main.js
- Assets\
- Source\
- Vendor\
and let's run the command:
C:\MyProject> DIR /AD /B | FIND /C /V ""
The command returns a single number representing the count of the subdirectories. The files are correctly ignored.
3
Storing the Count in a Variable
To use this count in a script, you need to capture the output into a variable. This is a perfect job for a FOR /F loop.
@ECHO OFF
SET "SubdirCount=0"
REM The FOR /F loop executes the command and captures its single-line output.
REM The pipe '|' must be escaped with a caret '^' inside a FOR /F command.
FOR /F %%C IN ('DIR /AD /B ^| FIND /C /V ""') DO (
SET "SubdirCount=%%C"
)
ECHO There are %SubdirCount% subdirectories in the current folder.
Output:
There are 3 subdirectories in the current folder.
Counting Recursively (All Subdirectories in a Tree)
Often, you need to count not just the immediate subdirectories but all folders within a directory tree.
The Solution is to use the /S switch.
The /S switch tells DIR to be Subversive (or recursive), searching the specified directory and all its subdirectories.
This script counts every single folder and subfolder within C:\Windows.
@ECHO OFF
SET "TotalFolders=0"
SET "TARGET_FOLDER=C:\Windows"
ECHO Counting all folders in "%TARGET_FOLDER%" and its subtrees...
ECHO This may take a moment...
FOR /F %%C IN ('DIR "%TARGET_FOLDER%" /AD /S /B ^| FIND /C /V ""') DO (
SET "TotalFolders=%%C"
)
ECHO Total number of folders found: %TotalFolders%
Common Pitfalls and How to Solve Them
Problem: The Count Includes Files
This is the most common mistake. If you forget the /AD switch, the DIR command will list both files and directories, giving you an incorrect total.
An example of the error:
REM This is WRONG. It will count the 2 files as well.
C:\MyProject> DIR /B | FIND /C /V ""
Output
# Output: Incorrectly reports 5 items.
5
Solution: Always use the /AD attribute switch to ensure you are only listing directories.
Problem: Hidden Folders are Ignored
By default, DIR /AD does not include hidden directories in its list. A folder might contain hidden subdirectories (like .git or .vs) that you want to include in your count.
Solution: Add the Hidden Attribute to the Switch
The /A switch can take multiple letters. To list directories, including hidden ones, you use /ADH.
- Attribute
- Directory
- Hidden
Script
@ECHO OFF
SET "SubdirCount=0"
ECHO Counting all subdirectories, including hidden ones...
REM Use /ADH to include hidden directories.
FOR /F %%C IN ('DIR /ADH /B ^| FIND /C /V ""') DO (
SET "SubdirCount=%%C"
)
ECHO Found %SubdirCount% subdirectories.
Practical Example: A Profile Folder Cleanup Check
This script checks a user's Downloads folder and reports a warning if the number of subdirectories exceeds a certain threshold, which could indicate a need for cleanup.
@ECHO OFF
SETLOCAL
REM --- Configuration ---
SET "DOWNLOADS_FOLDER=%USERPROFILE%\Downloads"
SET "WARN_THRESHOLD=50"
SET "FolderCount=0"
ECHO --- Downloads Folder Health Check ---
ECHO.
IF NOT EXIST "%DOWNLOADS_FOLDER%\" (
ECHO [ERROR] Downloads folder not found.
GOTO :End
)
ECHO Counting subdirectories in "%DOWNLOADS_FOLDER%"...
REM The most robust count: directories, including hidden ones.
FOR /F %%C IN ('DIR "%DOWNLOADS_FOLDER%" /ADH /B ^| FIND /C /V ""') DO (
SET "FolderCount=%%C"
)
ECHO Found %FolderCount% subdirectories.
IF %FolderCount% GTR %WARN_THRESHOLD% (
ECHO [WARNING] The number of subdirectories exceeds the threshold of %WARN_THRESHOLD%.
ECHO Consider cleaning up your Downloads folder.
) ELSE (
ECHO The number of subdirectories is within normal limits.
)
:End
ENDLOCAL
Conclusion
Counting subdirectories is a simple task once you know the correct DIR command switches. By filtering DIR's output, you can create a fast and reliable counting mechanism for your batch scripts.
For best results, remember these key points:
- Use
DIR /AD /Bas your base command to get a clean list of directories. - Pipe the output to
| FIND /C /V ""to get the final count. - Add the
/Sswitch toDIRto make your count recursive. - Add
Hto the attribute switch (/ADH) to include hidden folders in your count. - Wrap the command in a
FOR /Floop to capture the result into a variable.