Hello,
OVERVIEW:
I have needed a utility to do this for awhile, and was writing another AHK app which generates log files, and thought it would be nice to have a utility to delete files older than a certian date.
This utility can be run from a command line, or can be configured as a scheduled task in Windows.
I added a command line parameter to allow the user to specify directory recursion. So for example if you have a web server that generates logs in multiple directories, you can run a single command to clean up those files or you can create a scheduled task to do this every day.
I just tested this on a production web server with 23,000 files and it worked fine, cleaned up a ton of old junk.
It's kindof sloppy but just hammered it out. If anyone has any suggestions or input to make it better, please feel free to work on it. The core file recursion/deletion code was from another AHK forum post (sorry dont remember the author).
Hmm.. not sure how to attach a .zip file so i will post the .zip here:
http://www.okoboji.com/don/Delete_Older_v2.zip
README:
Del_Older2.exe is a tool to cleanup old files.
Written by donv/thinix - You can use this freely for non-commercial applications (dont sell this), but No Warranty - use at your own risk.
This tool allows wildcards to be specified, and a date to be specified. This allows log files to be
cleaned up that are older than XX days.
I just added the 'S' parameter option which will allow you to specify all subdirectories. For example
in a web server where each website stores log files in directories under a parent directory,
you can specify the parent directory of the logs, and this tool will recurse through all sub directories
and prune (delete) files older than the number of days you specify.
LIMITATIONS:
Just so this isn’t so dangerous, I just restricted this to only delete files ending with .log
If you need to use this to delete other types of files, please contact:
donv@rdi1.com.
SYNTAX:
at a dos prompt:
del_older2 rootdir fspec age P
where:
rootdir ;is the root of the log file folder, such as c:\inetpub\logs
fspec ;is typically specified as: "en*.log"
age ;is number of days older than todays date
;specifying 30 will delete files older than 30 days prior to todays date
P ;if P=Y (that means just do it, dont prompt)
;if P=P (that means prompt the user to confirm the file deletion
operation)
;if P=S (that means dont prompt the user to confirm the file deletion
operation, and recurse into all subdirectories below rootdir)
EXAMPLES:
del_older2 "c:\inetpub\log files" "ex*.log" 14 S
; will delete all files named: ex*.log which are older than 15 days and the
; S parameter means 'just do it - dont ask the user'
; AND this will recurse into ALL subdirectories below rootdir
del_older2 " c:\inetpub\log files" "ex*.log" 14 Y
; will delete all files named: log*.log which are older than 14
; days and will not ask the user for confirmation.
del_older2 " c:\inetpub\log files" "ex*.log" 14 P
; will delete all files named: log*.log which are older than 14
; days and will ask the user for confirmation before deleting
; files.
NOTES:
Quotes around the "rootdir" and "fspec" parameters are required if there is a space in the path or filenames
SCHEDULED TASK:
if you read the readme file in the .zip i have some notes that will be very helpful in getting this to run as a scheduled task.
------------ code for the delete older files utility -----------------
Code:
/*
syntax: del_older rootdir fspec age Y
This application deletes files older than a certain age.
*/
; del_older2 "c:\temp\test logs" "ex*.log" 15 S
CHECK1:
if 1 =
{
Goto, DISPMSG
}
Check2:
if 2 =
{
Goto, DISPMSG
}
IfNotInString, 2, .log
{
msgbox,0,Del_Older,Sorry this version is restricted to only allow deletion of .log files.`n`nIf you need to delete other file types`nPlease contact Thinix at: 800-659-3529
ExitApp
}
CHECK3:
if 3 =
{
Goto, DISPMSG
}
IF 3 < 0
{
msgbox, 0, Del_Older, Number of days should be between 1 and 999
ExitApp
}
IF 3 = 0
{
msgbox, 0, Del_Older, Number of days should be between 1 and 999
ExitApp
}
IF 3 > 999
{
msgbox, 0, Del_Older, Number of days should be between 1 and 999
ExitApp
}
difdays = %3%
;msgbox, difdays:%difdays%
CHECK4:
if 4 =P
{
msgbox, 4, Del_Older, Are you sure you want to delete all files matching this pattern:`n`n%1%\%2%`n`nwhich are older than:%3% days? (Y/N)
IfMsgBox Yes
{
Goto, DOIT
}
ExitApp
}
if 4 =S
{
ROOTPATH = %1%
filedelete, %A_ScriptDir%\dirlist.txt
RunWait, %comspec% /c dir %rootpath% /AD /B /S > dirlist.txt,%A_ScriptDir%, Hide UseErrorLevel
FileAppend %rootpath%, %A_ScriptDir%\dirlist.txt ; appends the root dir to dirlist (otherwise this will skip the specified directory itself)
;RUN, notepad %A_ScriptDir%\dirlist.txt
Loop
{
FileReadLine, line, %A_ScriptDir%\dirlist.txt, %A_Index%
if ErrorLevel ; reached end of dirlist - exit
{
ExitApp
}
;msgbox, current directory:%line%
Source = %line%\%2%
Time := A_Now
Loop, %Source%, 0, 1
{
TimeStamp := Time
EnvSub, TimeStamp, %A_LoopFileTimeModified%, Days
If ( TimeStamp >= difdays )
FileDelete, %A_LoopFileLongPath%
continue
}
continue
}
ExitApp
}
GOTO, DOIT
DISPMSG:
msgbox, 0, Del_Older,Syntax should look like this:`nDel_Older rootdir fspec age X`n`nFor Example:`nDel_Older "C:\temp\testlogs" "ex*.log" 14 Y
ExitApp
DOIT:
;msgbox, 0, Del_Older, del_older %1% %2% %3%
Source = %1%\%2%
Time := A_Now
Loop, %Source%, 0, 1
{
TimeStamp := Time
EnvSub, TimeStamp, %A_LoopFileTimeModified%, Days
If ( TimeStamp >= difdays )
FileDelete, %A_LoopFileLongPath%
}