Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Does anyone have a script that will allow me to take the contents of a directory and delete files based on their age?
Here's the scenerio. I have a directory that fills with report files. The files have about 30 unique names and they build up pretty quickly. The files also have retention times that have to be considered. Some are only kept for 1 week, some are kept for 1 months, some 6 months, etc etc.
I am looking for a script/command that will scan the directory and if a files data = 30 days/60days/1 month, etc older than todays date, it deletes the file. Anyone know how to get me started? thanks.

Jason,
The find commands below will remove files based on filename pattern and age of datestamp in days. The age check is granular to the current moment. You need write permission on the directory. You can throw in a -user xxxxx predicate also.
cd
find . -name "abc*" -mtime +7 -exec rm {} \;
find . -name "xyz*" -mtime +30 exec rm {} \;James

@echo off
-------------------------
:: Description: Deletes files older than a specified number of days
:: ++ attributes not set
::
:: Author: Barry.Lawrence@btinternet.com
:: Date: 01/03/2002
:: Version: 1.0
::
:: Comments: first draft - please email comments and suggestions
:: Usage: DELFILES sourcedir numberofdays attributes
::
:: Known: currently assumes all filedates > 2000 though I am
:: sure with a little thought this could easily be overcome
::
:: would probably benifit from a few setlocals..
:: and all parameters could be made optional
::
:: Restrictions: none ..enjoy
-------------------------:: this program requires windows NT or 2000.
If not "%OS%"=="Windows_NT" goto INVALIDOS
:: check the command line
if /i "%1"=="" goto SYNTAX
if "%1"=="/?" goto HELP
if /i "%2"=="" goto SYNTAX
:: extract the date components (date format on machine should be UK format else transpose set day/month below
for /F "tokens=1-4 delims=/ " %%o in ('date /t') do (set day=%%p
set month=%%q
set year=%%r)
:: calculate the current date less the cutoff period specified on the command line
call :JULIAN %year% %month% %day%
set numberofdays=%2
set /a juliansysdate="juliandate-numberofdays+0":: build list of files in specified dir with|without appropriate bits set in date order
dir %1 /O-d /A%3 >templist.fil
if %errorlevel% EQU 2 goto TIDYUP
:: iterate over the filelist extracting the components needed
for /F "skip=5 tokens=1,3* delims= " %%i in (templist.fil) do (call :PROCESS_FILEDETAILS %1 %%i "%%k")
del templist.fil
echo %filesdeleted% file(s) deleted.
goto TIDYUP:PROCESS_FILEDETAILS
set fdir=%1
set fdate=%2
set fname=%3
:: assume the file date to be > than 2000
for /F "tokens=1-4 delims=/ " %%x in ("%fdate%") do (set dd=%%x
set mm=%%y
set yyyy= 20%%z)
call :JULIAN %yyyy% %mm% %dd%
:: compare the file date with the modified system date
if %mm%-1 NEQ -1 (
if not %fname% == "" (
if %juliandate% LEQ %juliansysdate% (echo %fdate% %juliandate% %juliansysdate%
set /a filesdeleted=%filesdeleted+1
)
)
)goto END
:JULIAN
:: Calculate Julian Date
:: ***** do not change the formula, unless you understand the implications *****set y=%1
set m=%2
set d=%3set /a A=%Y/100
set /a B=%A/4
set /a C="2-A+B+0"
set /a E=Y%+4716
set /a E=36525*%E
set /a E=%E/100
set /a F="M+1"
set /a F=306001*%F
set /a F=%F/10000
set /a JulianDate="C+D+E+F-15245/10+0"
goto END
:HELP
echo Deletes files older than a specified number of days that meet the attribute criteria
echo.
echo DELFILES sourcedirectory numberofdays [attributes]
echo.
echo attributes a Archive
echo h Hidden
echo r Read-only
echo s System
echo d Directories
echo - Prefix meaning not
echo.
echo.
echo DELFILES c:\temp 10 -a-h-r-s-d
echo delete all files older than 10 days from the c:\temp directory that do
echo not have the archive, hidden, read-only, system and directories flags set.
echo.
echo DELFILES c:\temp 0 a
echo delete all files older than sysdate from the c:\temp directory with the archive flag set.
echo.
echo.
echo This batch program requires Windows NT or Windows 2000.
echo.
echo DELFILES /? will display this help message.
echo.
goto END
:SYNTAX
echo The syntax of the command is incorrect.
echo.
goto END
:INVALIDOS
echo.
echo This program requires Windows NT or Windows 2000
echo.
goto END
:TIDYUP
set a=
set b=
set c=
set d=
set e=
set f=
set i=
set j=
set k=
set l=
set y=
set m=
set d=
set yyyy=
set mm=
set dd=
set JulianDate=
set JulianSysDate=
set NumberOfDays=
set fdir=
set fdate=
set fname=
set day=
set month=
set year=
set filesdeleted=
goto END
:END

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |