Work out directory sizes in gB and MB in a bat file

This little bat file is cobbled together from 3 other files that nearly achieved what I was after but none of them could do gB and MB

Basically, this will scan a folder and give the total size of all files and folders within it and return the output in MB and gB as opposed to the bat / dos usual which is just in bytes!

Just save the following as size.bat and run it as follows
size.bat path\to\directory

example

size.bat c:\windows

Here’s the code

:size
set savepath=%1
@echo off
setLocal EnableDelayedExpansion
set /a value=0
set /a sum=0
FOR /R %savepath% %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
set /a meg=!sum!/1024
set /a gig=!meg!/1024
REM This section shows the following message if
REM the size if over 1gB
REM It also writes the information to the file
REM dirsize.txt in the root of the folder scanned
if %gig% LSS 1 goto undergig
echo Total backup is approx !gig!gB or to be more precise !meg!MB >> "%savepath%\dirsize.txt"
goto end

:undergig
REM this section just displays the following message
REM if the directory size is under 1gb
REM it also writes the information to the file
REM dirsize.txt in the root of the folder scanned
echo Total backup is under a gB or to be more precise !meg!MB >> "%savepath%\dirsize.txt"
goto end

:end
pause
Print Friendly, PDF & Email

More Like This


Categories


Windows

Tags


  • Post a comment