[THIN] Deleting files X days old

  • From: "David Demers" <david.demers@xxxxxxxxxxxxxxx>
  • To: "Thin (E-mail)" <thin@xxxxxxxxxxxxx>
  • Date: Thu, 21 Oct 2004 08:48:57 -0600

Hey guys,

I came across a need to regularly delete files over a certain age from a 
folder. And while I am aware that there are better ways to acomplish this task, 
I had some fun making this work in .bat form. I'm posting it here incase 
someone else might find it usefull.

The limitations of it are:
-- It doesn't care what year it is, which isn't an issue in my usage of it 
(regular pruning) as I will start with a manually cleaned folder.
-- During the first few days of March (dependant on how far back you want to 
retain) it will think the files are a day younger than they really are during 
leap years. Again not a big issue for my usage.

Other than that it seams sound. (Tested in Win2000 environment)

Usage:
Creat a scheduled task to kick off the file with the syntax of:

 Del_old Days "Folder"

Note: Not sure how text wrapping will effect this post.

============ Del_old.bat ================
@echo off
if {%2}=={} @echo Syntax:  Del_old.bat Days Folder&goto :EOF

SETLOCAL
REM ==== Grab Current Date ====
FOR /f "tokens=2-4 skip=1 delims=(-)" %%G IN ('echo.^|date') DO (
   FOR /f "tokens=2 delims= " %%A IN ('date /t') DO (
      SET v_first=%%G
      SET v_second=%%H
      SET v_third=%%I
      SET v_all=%%A
   )
)

SET %v_first%=%v_all:~0,2%
SET %v_second%=%v_all:~3,2%
SET %v_third%=%v_all:~6,4%
REM ==== The SET's above will be %mm% %dd% %yy% ====

REM ==== Incorporate Command Line Paramaters ====
SET days_old=%1
SET folder=%2#
SET folder=%folder:"=%
SET folder=%folder:\#=%
SET folder=%folder:#=%

REM ==== Convert to numerical date and assign Cut Off Date ====
CALL :JULIAN
echo Today's effective Julian Date: %julian_date%
SET /A cut_off_date=%julian_date%-%days_old%
echo Cut off date: %cut_off_date%

REM ==== Step through files in the directory ====
pushd "%folder%"
for /f "Tokens=*" %%G in ('dir /a /a-d /b') do (
 set file=%%G
 call :RESEARCH
)
popd
GOTO:EOF

REM ==== Sub Routine to determine numerical date ====
:JULIAN
IF /I "%mm%" EQU "01" SET month_in_days=365
IF /I "%mm%" EQU "02" SET month_in_days=31
IF /I "%mm%" EQU "03" SET month_in_days=59
IF /I "%mm%" EQU "04" SET month_in_days=90
IF /I "%mm%" EQU "05" SET month_in_days=120
IF /I "%mm%" EQU "06" SET month_in_days=151
IF /I "%mm%" EQU "07" SET month_in_days=181
IF /I "%mm%" EQU "08" SET month_in_days=212
IF /I "%mm%" EQU "09" SET month_in_days=243
IF /I "%mm%" EQU "10" SET month_in_days=273
IF /I "%mm%" EQU "11" SET month_in_days=304
IF /I "%mm%" EQU "12" SET month_in_days=334
SET /A julian_date=%month_in_days%+%dd%
GOTO:EOF

REM ==== Sub Routine to find date of files ====
:RESEARCH
for /f "Tokens=1-3 Delims=/ " %%a in ('dir "%file%" /a /tw^|Findstr /c:"/"') do 
(
 set mm=%%a
 set dd=%%b
 call :JULIAN
 call :PERFORM
)
GOTO:EOF

REM ==== Sub Routine to delete files if necessary ====
:PERFORM
Echo Date: %julian_date% File: %file%
IF /i %julian_date% LEQ %cut_off_date% (
   echo Deleting file %file%
   del /q /f "%file%"
)
GOTO:EOF

ENDLOCAL

********************************************************
This Weeks Sponsor RTO Software
Do you know which applications are abusing your CPU and memory?
Would you like to learn? --   Free for a limited time!
Get the RTO Performance Analyzer to quickly learn the applications, users,
and time of day possible problems exist.
http://www.rtosoft.com/enter.asp?id20
**********************************************************
Useful Thin Client Computing Links are available at:
http://thin.net/links.cfm
***********************************************************
For Archives, to Unsubscribe, Subscribe or
set Digest or Vacation mode use the below link:
http://thin.net/citrixlist.cfm

Other related posts: