- 1 year ago
batch - Script for filesize in GB for a specific folder
To create a batch script that calculates the total file size in GB for a specific folder, you can use a combination of the dir
command and some simple arithmetic operations. Here's an example script:
batch@echo off
setlocal
set "folder=C:\Path\to\your\folder"
set "totalSize=0"
for /f "tokens=3" %%a in ('dir /a-d /s /-c "%folder%" ^| findstr /r /c:"^ *([0-9][0-9]*[,.])*[0-9][0-9]* "') do (
set "size=%%a"
setlocal enabledelayedexpansion
set "size=!size:,=!"
set /a "totalSize+=!size!"
endlocal
)
set /a "totalSizeGB=totalSize / (1024*1024*1024)"
echo Total file size: %totalSizeGB% GB
endlocal
Replace "C:\Path\to\your\folder"
with the actual path to your target folder. The script uses a loop to iterate through the output of the dir
command, which lists all the files in the specified folder and its subfolders. It extracts the file size from the output and accumulates it in the totalSize
variable.
The size is then divided by 1024*1024*1024
to convert it to GB, and the result is stored in the totalSizeGB
variable. Finally, the script echoes the total file size in GB.
Save the script with a .bat
extension, and when you run it, it will calculate and display the total file size in GB for the specified folder.
Note that this script does not handle files with sizes greater than 2 GB due to limitations in batch scripting. If you have such large files, you may need to consider alternative scripting languages or tools that can handle larger sizes.