Jump to content

Delete specific files that are older than a week.


Recommended Posts

So are you able to define the exact "format" of the files you DO want to delete using wildcards? For example, could you use something like this:

forfiles -p "s:\" -s -m "specs08_??????_speclog.log" -d -30 -c "cmd /C dir @FILE"
specs08_??????_speclog.log : where the ?????? are there could be any number or alpha character.

But to answer your other question, I can define the format to this point "specs08_??????_speclog.log" but I only want to delete the files that are 7 days old and older. So it becomes a date created issue.

When I do a forfiles /? under a cmd prompt I get "not recognized". But if what you are hinting at is true it seems like it could be a viable way to delete the files.

My laptop is Xp Home, but I can try it on my other system if forfiles is on pro. Thanks.

Edit: Found the CMD "For" that must be what you were talking about.

Edited by schilbiz
Link to comment
Share on other sites

@ weaponx

Your script works fine but I don't see how I can adjust it to not delete all the files in the folder that are older then the days specified. As I have mentioned there is only a couple original filenames that I want to remove when they are older than 7 days. The only changes in the file names are the 6 numbers in the middle of the filename. Sorry if I did not explain it well enough, e.g. specs08_111098_speclog.log

If it would delete all files with this format specs08_??????_speclog.log that are older then 7 days. That is what I am trying to accomplish.

This works great in itself.

#include <file.au3>
#include <date.au3>

;Root folder
$sourceFolder = "C:\logfiles"

;Gather files into an array
$fileList = _FileListToArray($sourceFolder, "*.*", 1)

;Loop through array
For $X = 1 to $fileList[0]
    ;Retrireve creation time of file
    $Date = FileGetTime($sourceFolder & "\" & $fileList[$X], 1, 0)
    
    ;Format date for use with Date UDF
    $fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0],$Date[1],$Date[2],$Date[3],$Date[4],$Date[5])
    
    ;Calculate age, remove files older than one week
    If _DateDiff('d', $fDate,_NowCalc()) > 0 Then Msgbox(1, "Would have deleted:", $fileList[$X])

Next
Link to comment
Share on other sites

You just modify the search pattern:

$fileList = _FileListToArray($sourceFolder, "*.*", 1)

--------->

$fileList = _FileListToArray($sourceFolder, "specs08_*_speclog.log", 1)

Interesting, I am pretty sure I tried that.. but I tried it again and it works fine, Thanks a lot man.

Link to comment
Share on other sites

specs08_??????_speclog.log : where the ?????? are there could be any number or alpha character.

But to answer your other question, I can define the format to this point "specs08_??????_speclog.log" but I only want to delete the files that are 7 days old and older. So it becomes a date created issue.

When I do a forfiles /? under a cmd prompt I get "not recognized". But if what you are hinting at is true it seems like it could be a viable way to delete the files.

My laptop is Xp Home, but I can try it on my other system if forfiles is on pro. Thanks.

Edit: Found the CMD "For" that must be what you were talking about.

Nope... Not "For"... FORFILES (a free Microsoft utility) handles recursion and does all the date calculations for you... :D Simply change the parameter "30" in the example to "7". I did not test your particular wild card string - but it should work fine. I use this method on hundreds of servers to clean up old (stale) log files via a batch job / scheduled task. But, as always, test carefully before using in production.

A detailed description of all of the features of FORFILES can be found at:

http://technet2.microsoft.com/windowsserve...3.mspx?mfr=true

If you don't have it, you can download the latest version of FORFILES from the "Windows 2003 Server Resource Kit"... That version is the "latest" and works with Windows XP. (Nothing to "install" on your computer... Just run the executable...) Here is a Microsoft FTP link that includes the version of FORFILES.EXE that will work on XP.

ftp://ftp.microsoft.com/reskit/y2kfix/x86/

Link to comment
Share on other sites

Thanks UmmDoughnuts I will look into FORFILES for future use, for now weaponx's solution seems to do the trick, its "lite" and fast I like it. Also thanks Martin, d4rk and GEOSoft for the suggestions. Heres the code I ended up using.

#include <file.au3>
#include <date.au3>

;Root folder
$sourceFolder = "\\server\share\"

;Gather files into an array
$fileList = _FileListToArray($sourceFolder, "spec08_*_speclogs_PDF.log", 1)

;Loop through array
For $X = 1 to $fileList[0]
    
;Retrieve creation time of file
    $Date = FileGetTime($sourceFolder & "\" & $fileList[$X], 1, 0)
    
;Format date for use with Date UDF
    $fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0],$Date[1],$Date[2],$Date[3],$Date[4],$Date[5])
    
;Calculate age, remove files older than one week
    If _DateDiff('d', $fDate,_NowCalc()) > 7 Then FileDelete($sourceFolder & "\" & $fileList[$X])
    Msgbox(1, "Files deleted:", $fileList[$X], 1)
Next
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...