For a batch solution
@echo off
setlocal enableextensions disabledelayedexpansion
rem 12345678901234567890
set "pad= "
for %%a in ("%~f1.") do set "target=%%~fa"
set "limit=%~2"
if not defined limit set "limit=10737418240"
echo(Searching in [%target%] ....
for /f "tokens=3" %%a in ('
dir /s /-c /a "%target%"
^| findstr /i /r /c:"^ .*bytes$"
') do set "size=%%a"
set "limit=%pad%%limit%"
set "size=%pad%%size%"
echo(
echo(.... limit [%limit:~-20%]
echo(.... size [%size:~-20%]
echo(
if "%size:~-20%" lss "%limit:~-20%" (
echo Size is in range
goto :eof
)
echo(Size is out of range. Cleaning ....
pushd "%target%" && (
rem rmdir . /s /q
popd
)
This batch file uses two arguments: the first one is the folder to process (by default the current folder) and the second one the size limit to check (by default 10GB).
A dir
command in used to retrieve the full size under the indicated folder. The ouput from this comand is parsed with a for /f
to get the needed data.
Batch files arithmetic is limited to a 2^31 max value. To handle greater values in conditions, the numbers need to be converted to padded strings to be compared.
The script only echoes information to console. You will need to uncomment the rmdir
command that should remove the contents of the target folder.