Andreas
Andreas

Reputation: 2047

Automatically run a batch file when disk space gets low

I'm writing a batch script to automatically remove specific folders older then 7 days to keep my disk from getting full. Is there any way to automate this so that it will run when free disk space is below 2GB? Or do I just have to automate the task and let it check in startup wether the requirement to run is met?

Upvotes: 0

Views: 3875

Answers (2)

Vaguiner Gonzalez
Vaguiner Gonzalez

Reputation: 99

@echo off
set space_cmd=2147483648
:loop
FOR /F "delims=" %%i IN ('dir c:\') DO set space=%%i
set process_01=%space%
set process_02=%process_01:~-26%
set process_03=%process_02:.=%
set process_04=%process_03: bytes free=%
set result=%process_04%
cls
echo.
echo.
title %result%/%space_cmd%
echo Free space: %result%
echo Limit: %space_cmd%
if "%result%" LEQ "%space_cmd%" goto command
ping -n 2 127.0.0.1>nul
goto loop
:command
mkdir c:\temp_7_days_older
robocopy c:\* c:\temp_7_days_older /move /minage:7 /maxage:10
rmdir /q /s c:\temp_7_days_older
goto loop

try this

Upvotes: 1

foxidrive
foxidrive

Reputation: 41234

You can schedule a script to run periodically in task scheduler.

For a script to delete files when disk space gets low, it needs to be running all the time and checking periodically itself.

Upvotes: 0

Related Questions