How to Convert File Size to Human-Readable Format in Batch Script
Raw file sizes are usually reported in Bytes (e.g., 1073741824). For humans, it is much easier to read this as 1 GB. Converting a large raw integer into a human-readable format (KB, MB, GB) requires a sequence of divisions by 1024. In Batch, we can use a loop to "Step up" through the units until the number is small enough to be easily understood.
In this guide, we will demonstrate how to build a dynamic file-size formatter.
The Strategy: The 1024 Staircase
- Start with the raw
Bytes. - If the number is greater than 1024, divide by 1024 and move to
KB. - If it's still greater than 1024, divide again and move to
MB. - If it's still greater than 1024, divide again and move to
GB.
Implementation Script
@echo off
setlocal enabledelayedexpansion
:: 1. Input: Raw bytes (Example: 1.5 GB in bytes)
set "rawSize=1610612736"
:: 2. Define Units
set "U1=Bytes"
set "U2=KB"
set "U3=MB"
set "U4=GB"
set "U5=TB"
set "unitIdx=1"
set "printableSize=%rawSize%"
echo Converting raw size: %rawSize% bytes...
:: 3. The Scaling Loop
:scale_loop
if !printableSize! GTR 1024 (
if !unitIdx! LSS 5 (
set /a "printableSize=printableSize / 1024"
set /a "unitIdx+=1"
goto scale_loop
)
)
:: 4. Get the final Unit string
set "finalUnit=!U%unitIdx%!"
echo.
echo ==========================================
echo FORMATTED SIZE: !printableSize! !finalUnit!
echo ==========================================
pause
endlocal
The final output uses !printableSize! and !finalUnit! (delayed expansion) instead of %printableSize% and %finalUnit%. While percent expansion would work here because the echo lines sit outside any parenthesized block, using delayed expansion consistently throughout a script that enables it avoids subtle bugs if the code is later refactored into a block.
Why Convert File Sizes?
- Reporting: When generating a disk usage report for an end-user,
2 MBis much more professional and understandable than2097152 bytes. - Quota Alerts: Sending an automated email when a user's folder exceeds "10 GB" is more impactful than using a raw byte count.
- Log Scanning: Quickly scanning a directory list to find "The Big Files" is easier when they are categorized by unit size.
Important Limitations
- 32-Bit Limit: Batch is limited to 2,147,483,647. This means this script cannot process file sizes larger than ~2 GB using raw division. For files larger than 2 GB, you must use a string-manipulation trick or PowerShell.
- No Decimals: If a file is
1.5 MB, this script will show1 MBbecause Batch truncates decimals. - Accuracy: Each division loses a bit of precision because of integer truncation.
Pro Tip: Handling Files Larger than 2 GB (PowerShell)
Since modern files (like 4K videos or ISOs) are often larger than 2 GB, the native Batch set /a division will fail. Use this PowerShell bridge instead:
@echo off
setlocal
:: Get file size of 'myvideo.mp4' in GB
set "filepath=myvideo.mp4"
for /f "usebackq delims=" %%r in (`powershell -NoProfile -Command "$s = (Get-Item '%filepath%').Length; '{0:N2} GB' -f ($s / 1GB)"`) do set "result=%%r"
echo %filepath%: %result%
pause
endlocal
Wrapping the powershell call in a for /f loop captures the output into a Batch variable so you can use the formatted size elsewhere in your script. The -NoProfile flag speeds up execution by skipping the user's PowerShell profile.
Conclusion
Formatting file sizes makes your administrative scripts feel professional and user-friendly. By implementing the "1024 Staircase" logic, you transform raw, confusing system metrics into clear, actionable data. While Batch integer math has hard limits at 2 GB, the fundamental logic remains essential for directory audits and storage monitoring in any Windows support environment.