Skip to main content

How to Get the Size of a Folder (and subfolders) in Batch Script

A common task in disk management and system cleanup is determining how much space a folder and all of its contents are using. While you can easily see this by right-clicking a folder in Windows Explorer, this action isn't available to a batch script. To get this information programmatically, a script must perform the calculation manually: it needs to find every single file within the directory tree and add up their individual sizes.

This guide will teach you the standard, pure-batch method for calculating a folder's total size by combining a recursive DIR command with a FOR loop. You will learn the core logic, the critical limitations of this method (especially with large folders), and see a practical example of a script that generates a size report for all subdirectories in a given path.

The Challenge: No Direct "Folder Size" Command

Windows cmd.exe has no built-in command to calculate the cumulative size of a directory. The DIR command lists the sizes of individual files, but it does not provide a total for a folder. Therefore, our script must do the work itself:

  1. Recursively find every file.
  2. Get the size of each file.
  3. Sum all the sizes together.

The Core Method: Looping with DIR and FOR

The most effective way to do this is to get a clean, recursive list of all files and then process that list.

  • DIR /S /A-D /B: This command is the key to getting the file list.
    • /S: Makes the search Subversive (recursive).
    • /A-D: Filters the list to show items that are Not Directories (i.e., files only).
    • /B: Provides a Bare format, listing just the full path of each file, which is perfect for parsing.
  • FOR /F: This loop will process the list generated by DIR one file at a time.
  • %%~zF: Inside the loop, this modifier gets the size of the current file.
  • SET /A: This command performs the Arithmetic addition.

The Script: Getting the Total Size in Bytes

This script implements the logic above to calculate the total size of a specified folder.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "TARGET_FOLDER=C:\Users\Admin\Documents"
SET "totalSize=0"

ECHO Scanning folder: "%TARGET_FOLDER%"...
ECHO This may take a while for large folders.

REM The FOR /F loop processes the output of the DIR command.
FOR /F "delims=" %%F IN ('DIR "%TARGET_FOLDER%" /S /A-D /B') DO (
REM For each file found, get its size and add it to the total.
SET /A "totalSize+=%%~zF"
)

ECHO.
ECHO --- Calculation Complete ---
ECHO Folder: "%TARGET_FOLDER%"
ECHO Total Size: !totalSize! bytes

ENDLOCAL

How the script works:

  • SETLOCAL ENABLEDELAYEDEXPANSION: This is absolutely essential. It allows us to use !totalSize! to get the updated value of the variable inside the loop. Using %totalSize% would not work correctly.
  • FOR /F "delims=" %%F IN ('...'): This executes the DIR command and begins looping through its output line by line, with each line (a full file path) being stored in the %%F variable.
  • SET /A "totalSize+=%%~zF": This is the heart of the script.
    • SET /A: Tells the command processor to perform an arithmetic calculation.
    • totalSize+=...: This is shorthand for totalSize = totalSize + ....
    • %%~zF: This is the special parameter expansion that retrieves the size in bytes of the file currently stored in %%F.

Common Pitfalls and How to Solve Them

Critical Limitation: The 2GB Maximum

This is the biggest and most important pitfall of this pure-batch method. The SET /A command in batch uses 32-bit signed integer arithmetic. This means the maximum number it can handle is 2,147,483,647 (which is 2 GB).

If the total size of your folder exceeds this limit, the totalSize variable will overflow and wrap around to a negative number, giving you a completely wrong result.

Solution: For any folder that might be larger than 2 GB, the pure-batch method is unreliable. The correct and modern solution is to use PowerShell, which handles large numbers (64-bit integers) correctly.

powershell -Command "Get-ChildItem -Path '%TARGET_FOLDER%' -Recurse -File | Measure-Object -Property Length -Sum | Select-Object -ExpandProperty Sum"

This PowerShell one-liner is faster, more robust, and the recommended approach for large-scale tasks.

Problem: Performance on Large or Slow Drives

The script needs to access the metadata for every single file in the directory tree. On drives with hundreds of thousands of files or on slow network shares, this process can take a very long time.

Solution: This is an inherent limitation of the task. Be as specific as possible with your TARGET_FOLDER to limit the scope of the scan.

Problem: The Script Lacks Permissions

If the script encounters a folder it doesn't have permission to read (like another user's profile), it will display an "Access is denied" error for that folder and skip its contents.

Solution: For a comprehensive scan of a drive, run your script as an Administrator. This will grant it the necessary privileges to access most system and user folders.

Practical Example: A Subdirectory Size Report

This script is a very useful utility. It scans a target path and calculates the total size of each subdirectory within it, giving you a clear report of which folders are consuming the most space.

@ECHO OFF
SETLOCAL
SET "SCAN_ROOT=C:\Program Files"

ECHO --- Subdirectory Size Report for "%SCAN_ROOT%" ---
ECHO.

REM The /D switch loops through directories.
FOR /D %%D IN ("%SCAN_ROOT%\*") DO (
CALL :GetFolderSize "%%D"
)
GOTO :EOF


:GetFolderSize
SETLOCAL ENABLEDELAYEDEXPANSION
SET "folder=%~1"
SET "size=0"
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
note

This script uses a callable subroutine (:GetFolderSize) to perform the calculation for each directory found by the main FOR /D loop.

Conclusion

Calculating a folder's size in a batch script is a powerful technique for disk space analysis. The standard method involves recursively listing all files with DIR /S /A-D /B and summing their sizes in a FOR /F loop.

However, you must be aware of the critical limitations:

  • The pure-batch method is unreliable for folders larger than 2 GB due to 32-bit integer limits.
  • For large folders, or for any critical task, using the PowerShell equivalent is the faster, safer, and more robust solution.