booyaa dot org
i wrote this batch file to avoid the excessive build up of temp files created by fax software. because the application didn’t tidy up after itself, after a while (about 300K temp files later) it started to freak out and much oddness occurred as it tried to write subsequent temp files.
it takes one parameter, the number of days to before a file is considered old. the default is 30. the script will look for the existence of a file called fileReaper.lst (see sample below), this is a text file containing a list of directories that file reaper will run against, otherwise it will just run against the current directory.
the script is happy to be run by the task scheduler.
fileReaper.lst:
C:\DOCUME~1\SVC_RI~1\LOCALS~1\Temp\ C:\WINNT\TEMP C:\TEMP\ H:\TEMP\
fileReaper.bat:
@ECHO OFF
CLS
SETLOCAL ENABLEDELAYEDEXPANSION
:: filereaper - kills files older than a N days
:: If no parameter given assume target files are older than 30 days
IF "%1%" == "" (
SET paraDaysToAge=30
) ELSE (
SET paraDaysToAge=%1%
)
::******************************************************
::*** NO USER SERVICABLE PARTS BELOW THIS POINT
::******************************************************
::Grab the current date
FOR /F "tokens=1-3 delims=/ " %%A IN ('DATE /T') DO (
SET tDD=%%A
SET tMM=%%B
SET tYYYY=%%C
)
IF NOT EXIST fileReaper.lst. GOTO noFileList
:: Main loop with reaper list file
FOR /F %%A IN (fileReaper.lst) DO (
SET dirList=%%A
IF EXIST %%ANUL (
:: Reaper loop
FOR /F "tokens=1-3,6 delims=/ " %%B IN ('DIR /A:-D /O:D !dirList! ^| FINDSTR /R /C:"^[0-9][0-9]/"') DO (
SET fDD=%%B
SET fMM=%%C
SET fYYYY=%%D
SET fName=%%E
SET /A fAge=!tYYYY!!tMM!!tDD!-!fYYYY!!fMM!!fDD!
IF !fAge! GEQ !paraDaysToAge! (
ECHO !dirList!!fName! is !fAge! days old, in scope for deletion
)
)
) ELSE (
ECHO ERROR: !dirList! does not exist!
)
)
GOTO theEnd
:noFileList
:: Main loop with no file list provided, assuming current working directory
FOR /F "tokens=1-3,6 delims=/ " %%B IN ('DIR /A:-D /O:D ^| FINDSTR /R /C:"^[0-9][0-9]/"') DO (
SET fDD=%%B
SET fMM=%%C
SET fYYYY=%%D
SET fName=%%E
SET /A fAge=!tYYYY!!tMM!!tDD!-!fYYYY!!fMM!!fDD!
IF !fAge! GEQ !paraDaysToAge! (
ECHO !dirList!!fName! is !fAge! days old, in scope for deletion
)
)
GOTO theEnd
:theEnd




New Blog Post: batchacha: file reaper – i wrote this batch file to avoid the excessive build up of temp files creat… http://ow.ly/16LQ1l