Jun 3, 2013

How to delete oldest files when a folder size goes above limit.

I needed to delete surveillance video files once I have more than certain amount in my hard drive. Let's say I don't want to hold more than 2GByte of video files, which will probably cover  one or two month of surveillance video data.

What I need to do with a batch file is like this:
  1. Check how much video files are collected so far.
  2. Compare if the total size is bigger than the size I can afford.
  3. If it is under the space budget, then the batch finishes.
  4. If there are more than I can afford, I want to delete oldest files first.
  5. So I need to figure out what date/time is the oldest.
  6. I will delete files that are at the date/time.
  7. Go to the first step in order to see if I need to delete more.

This was little tricky without installing any external applications.

I had to make two separate batch files due to the syntax limitations on For loop.
One looks like this (c:\batch\delete_surveillance_find_oldest.bat):
@ECHO OFF
SET fdate=%1
SET fdate=%fdate:~6,4%%fdate:~0,2%%fdate:~3,2%
SET /a DATE_DIFF=%OLDEST_DATE% - %fdate%
IF NOT "x%DATE_DIFF:~0,1%" == "x-" SET OLDEST_DATE=%fdate%
It takes a Date value as an input argument.
It compares the date with an oldest date.
If it the date is older than the one that has been found, it updates it.

Another file is like this (c:\batch\delete_surveillance.bat):
@ECHO OFF
SET MAX_SIZE_IN_MB=1800
SET TargetDir=C:\surveillance
SET FIND_OLDEST=C:\batch\delete_surveillance_find_oldest.bat

IF NOT EXIST "%TargetDir%" GOTO :ERROR_ARG
IF NOT EXIST "%FIND_OLDEST%" GOTO :ERROR_ARG

REM SET DeleteCmd=ECHO
SET DeleteCmd=DEL /f

:RESTART
SET TotalSize=0
FOR /F "tokens=1,2,3" %%A IN (' dir /s "%TargetDir%" ^| FIND " File(s)" ') DO SET TotalSize=%%C
SET /a TotalSize=%TotalSize:,=%/1024/1024
ECHO Size limit: %MAX_SIZE_IN_MB%
ECHO Total size: %TotalSize% MB

SET /a SizeDiff=%MAX_SIZE_IN_MB% - %TotalSize%
IF NOT "x%SizeDiff:~0,1%" == "x-" GOTO :SPACE_ENOUGH

ECHO Space is not Enough...
ECHO Searching for the date of the oldest file...
SET /a OLDEST_DATE=99998877
FOR /F "tokens=1,2" %%A IN (' dir /s /a-d "%TargetDir%" ^| FIND "/" ' ) DO CALL "%FIND_OLDEST%" %%A
ECHO Oldest date found: %OLDEST_DATE:~0,4%/%OLDEST_DATE:~4,2%/%OLDEST_DATE:~6,2%

ECHO Deleting the oldest files...
FORFILES /p %TargetDir% /s /D -%OLDEST_DATE:~4,2%/%OLDEST_DATE:~6,2%/%OLDEST_DATE:~0,4% /c "cmd /c @FOR %%I IN (@path) DO @IF NOT EXIST %%~sI\NUL ( @ECHO @path & %DeleteCmd% /f @path )"
ECHO Let's see if we have enough space now.
ECHO.
GOTO :RESTART

:SPACE_ENOUGH
ECHO There are enough space.
GOTO :END

:ERROR_ARG
ECHO Input arguments or settings are incorrect.
ECHO TargetDir = %TargetDir%
ECHO FIND_OLDEST = %FIND_OLDEST%
GOTO :END

:END

This will follow the process I described above.

3 comments:

Unknown said...

Thank you for the information, which is what I am looking for.

However, when following your instruction, I run into some problem. After created my batch file - record_foscam.bat and run it, it did catch a asf file, but it has audio but only one snapshot picture, not a video. Not sure where did I do wrong.

Any idea? Thanks a lot.

John

Jay said...

This article doesn't have "record_foscam" batch file. I am not sure which one you are referring to.

Unknown said...

Hi Jay,

when I run the script, always comes the result:

C: \ batch> delete_surveillance.bat
Size limit: 400
Total size: 0 MB
There are enough space.

The Odner is large in real 480MB. So the script ought to delete files.

The script can not evaluate the folder size?

best regards
Thomas