 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
donv
Joined: 18 Oct 2008 Posts: 18
|
Posted: Wed Apr 15, 2009 9:38 pm Post subject: Tool to delete (log) files older than specified date. |
|
|
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%
}
|
_________________ -d  |
|
| Back to top |
|
 |
mikeyes
Joined: 16 Nov 2009 Posts: 1
|
Posted: Mon Nov 16, 2009 7:09 am Post subject: Possible modification |
|
|
Thank you for the script. I was needing to perform this as a function in a script I was writing and this was a perfect road map.
One thing I found when working on my function was that I didn't need to build the dirlist.txt file even when recursing into sub directories.
If the user specifies that they want to recurse into sub directories you can use the 3rd option of the Loop command to do this.
Loop, FilePattern [, IncludeFolders?, Recurse?]
By specifying "Loop, FilePattern, 0, 1" your loop will recurse into all the sub directories without the need to build and read the dirlist.txt file.
Here is what I came up with:
| Code: | #NoEnv
OldConfigCleanup("2","C:\temp","*.txt")
OldConfigCleanup(Daystokeep,Pathtoconfigs,exttodelete)
{
difdays = %Daystokeep%
;msgbox, difdays:%difdays%
;msgbox, current directory:%Pathtoconfigs%
Source = %Pathtoconfigs%\%exttodelete%
;msgbox, source: %Source%
Time := A_Now
Loop, %Source%, 0, 1
{
TimeStamp := Time
EnvSub, TimeStamp, %A_LoopFileTimeModified%, Days
;msgbox, timestamp for file %A_LoopFileLongPath% is %TimeStamp%
If ( TimeStamp >= difdays )
{
;MsgBox, file to delete: %A_LoopFileLongPath%
FileDelete, %A_LoopFileLongPath%
}
continue
}
ExitApp
}
|
Hope that helps someone like your script helped me and thanks again. |
|
| Back to top |
|
 |
First Toy Lab
Joined: 15 Nov 2009 Posts: 21 Location: London, UK
|
|
| Back to top |
|
 |
Guest
|
Posted: Wed Jan 27, 2010 1:58 am Post subject: |
|
|
| Thanks for the helpful program! |
|
| Back to top |
|
 |
Dineshbabu Guest
|
Posted: Tue Nov 30, 2010 6:56 am Post subject: Good!!!!!! |
|
|
Nice & good to c this type of script......  |
|
| Back to top |
|
 |
cijovojs
Joined: 04 Nov 2011 Posts: 2
|
Posted: Fri Nov 04, 2011 3:55 pm Post subject: Re: Possible modification |
|
|
| mikeyes wrote: | Thank you for the script. I was needing to perform this as a function in a script I was writing and this was a perfect road map.
|
Thanks! I too found this a really helpful place to start as I needed a similar script. Borrowing from both previous contributors, here is what I came up with:
| Code: | /*
This application deletes files older than a certain numbers of days. Options to recurse and whether to ask first.
Syntax: DeleteFilesOlderThan "rootdir" "fspec" days recurse ask
*/
blnDeleteFiles := true
Source = %1%\%2%
; Number of days
if (3 < 1 OR 3 > 999)
{
msgbox, 0, %A_ScriptName%, Number of days should be between 1 and 999
ExitApp
}
difdays = %3%
; Recurse?
if 4 = Y
{
Subdir = 1
strText = (including those in sub-folders)
}
else
Subdir = 0
; Ask first?
if 5 = Y
{
msgbox, 4, %A_ScriptName%, Are you sure you want to delete all files %strText% `nmatching this pattern:`n`n%1%\%2%`n`nwhich are older than %3% days?
IfMsgBox Yes
blnDeleteFiles := true
Else
blnDeleteFiles := false
}
if (blnDeleteFiles)
{
Time := A_Now
Loop, %Source%, 0, %Subdir%
{
TimeStamp := Time
EnvSub, TimeStamp, %A_LoopFileTimeModified%, Days
;msgbox, %TimeStamp% %A_LoopFileTimeModified% %Days%
If ( TimeStamp >= difdays )
FileSetAttrib, -R-A, %A_LoopFileLongPath% ; To make sure the FileDelete command will work
FileDelete, %A_LoopFileLongPath%
;msgbox, deleted %A_LoopFileLongPath%
}
}
If ErrorLevel
MsgBox There was a problem running %A_ScriptName%
Else
FileAppend, %A_ScriptName% ran successfully at %A_Now%`n, %A_ScriptDir%\%A_ScriptName%.log
|
|
|
| Back to top |
|
 |
NelsonT191 Guest
|
Posted: Fri Jan 06, 2012 7:54 am Post subject: Help a script delete files |
|
|
Help
I wanted to delete a file. TXT whenever he
exist in a folder.
and the script is always active ... |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|