How to Get Available Disk Space in a Batch Script
Monitoring available disk space is a critical task for any system administrator or power user. A script might need to check if there is enough free space before starting a large file copy, a software installation, or a backup. Running out of disk space during an operation can lead to corrupted data and failed processes, so a pre-flight check is an essential part of robust automation.
This guide will teach you the modern, recommended method for getting disk space information using the powerful WMIC (Windows Management Instrumentation) command. You will learn how to get the free space and total size of a drive, how to capture these values into variables, and how to perform calculations with them.
The Challenge: No Simple "GetFreeSpace" Command
There is no direct command in batch like GETFREESPACE C:. A script needs to query the operating system's disk management layer to get this information. While there are several ways to do this, some are far more reliable and easier to parse than others.
The Modern Method (Recommended): Using WMIC
The WMIC (Windows Management Instrumentation Command-line) utility is the standard, powerful tool for querying all kinds of system information, including disk details. It can provide exact byte counts for a drive's size and free space in a clean, predictable format that is easy for a script to parse.
Command: WMIC LOGICALDISK WHERE "DeviceID='C:'" GET FreeSpace,Size /VALUE
LOGICALDISK: The WMI "class" that represents disk volumes.WHERE "DeviceID='C:'": A filter to select only the C: drive.GET FreeSpace,Size: Specifies the exact properties we want to retrieve./VALUE: Formats the output as a simpleKey=Valuelist, which is perfect for parsing.
The Legacy Method (To Avoid): Parsing DIR
A very old trick involves running the DIR command on a drive and then trying to parse the "bytes free" line at the bottom.
Why this is a bad idea:
- Brittle: The format of the
DIRcommand's output can change slightly between Windows versions or languages. A script that expects a specific text format can easily break. - Hard to Parse: The numbers in the
DIRoutput contain commas, which makes them difficult to use inSET /Acalculations. - Slow:
DIRhas to scan the directory table, whileWMICqueries the file system metadata directly.
For these reasons, the WMIC method is overwhelmingly superior.
The Script: Getting Free Space and Total Size with WMIC
This script uses the recommended WMIC method to get the details for the C: drive and stores them in variables.
@ECHO OFF
SETLOCAL
SET "DriveLetter=C:"
SET "FreeSpace="
SET "TotalSize="
ECHO --- Getting Disk Space for Drive %DriveLetter% ---
ECHO.
REM The FOR /F loop processes the Key=Value output from WMIC.
FOR /F "tokens=1,2 delims==" %%A IN (
'WMIC LOGICALDISK WHERE "DeviceID='%DriveLetter%'" GET FreeSpace^,Size /VALUE'
) DO (
REM Check which key we got and assign the value to the correct variable.
IF "%%A"=="FreeSpace" SET "FreeSpace=%%B"
IF "%%A"=="Size" SET "TotalSize=%%B"
)
IF NOT DEFINED FreeSpace (
ECHO [ERROR] Could not retrieve disk space information for drive %DriveLetter%.
) ELSE (
ECHO Total Size: %TotalSize% bytes
ECHO Free Space: %FreeSpace% bytes
)
ENDLOCAL
How theWMIC script above works
FOR /F "tokens=1,2 delims==" %%A IN ('...'): This is the core parsing logic.'WMIC ... /VALUE': This command is executed, and its output is captured. The output looks like this (with some blank lines):FreeSpace=129393303552
Size=511002210304tokens=1,2 delims==: This tells theFORloop to split each line by the=delimiter. The part before the=goes into%%A(the key), and the part after it goes into%%B(the value).
IF "%%A"=="FreeSpace" SET "FreeSpace=%%B": This checks the key. If it's "FreeSpace", it assigns the corresponding value to ourFreeSpacevariable.
Common Pitfalls and How to Solve Them
-
Large Numbers (Beyond 2GB): The values for disk size are in bytes and are often huge numbers, far exceeding the 2GB limit of the
SET /Acommand.- Solution: When performing calculations, you must first scale the numbers down. A common practice is to divide by
1048576(the number of bytes in a megabyte) to work with megabytes, or1073741824for gigabytes.
SET /A "FreeSpaceGB = %FreeSpace% / 1073741824"
ECHO Free Space: %FreeSpaceGB% GB (approx) - Solution: When performing calculations, you must first scale the numbers down. A common practice is to divide by
-
The Carriage Return Bug: On some systems, the output from
WMICincludes an extra carriage return character that gets appended to the value. This can break numerical comparisons.- Solution: A clever trick is to use another
FORloop to "clean" the variable. This re-assigns the value and strips any invisible trailing characters.
REM The value in %%B might be "12345<CR>"
IF "%%A"=="FreeSpace" SET "FreeSpaceRaw=%%B"
...
REM This FOR loop cleans the variable.
FOR %%N IN (%FreeSpaceRaw%) DO SET "FreeSpace=%%N" - Solution: A clever trick is to use another
Practical Example: A Pre-Installation Space Check
This script checks if the C: drive has at least 5 GB of free space before allowing a large installation to proceed.
@ECHO OFF
SETLOCAL
SET "RequiredGB=5"
SET /A "RequiredBytes = %RequiredGB% * 1073741824"
ECHO --- Disk Space Pre-Flight Check ---
ECHO Required free space: %RequiredGB% GB
ECHO.
REM --- Get the free space for the C: drive ---
SET "FreeSpace="
FOR /F "tokens=2 delims==" %%F IN (
'WMIC LOGICALDISK WHERE "DeviceID='C:'" GET FreeSpace /VALUE'
) DO (
SET "FreeSpaceRaw=%%F"
)
REM Clean the variable of any trailing characters
FOR %%N IN (%FreeSpaceRaw%) DO SET "FreeSpace=%%N"
ECHO Available free space: %FreeSpace% bytes
ECHO.
IF %FreeSpace% LSS %RequiredBytes% (
ECHO [FAILURE] Not enough free disk space.
ECHO Installation requires at least %RequiredGB% GB. Aborting.
) ELSE (
ECHO [SUCCESS] Sufficient disk space is available.
ECHO Starting installation...
REM (Installer command would go here)
)
ENDLOCAL
Conclusion
The WMIC command is the modern, reliable, and definitive tool for getting disk space information in a batch script.
- The core command is
WMIC LOGICALDISK WHERE "DeviceID='C:'" GET FreeSpace,Size /VALUE. - Use a
FOR /Floop to parse theKey=Valueoutput and store the results in variables. - Be mindful that the returned values are in bytes and are often very large, so you may need to scale them down for calculations to avoid
SET /A's 2GB limit.
By using WMIC, you can write robust scripts that make intelligent decisions based on the available disk space, preventing common installation and file copy failures.