RichardT Posted May 17, 2010 Share Posted May 17, 2010 Hello All, I am try to write a script that deletes files within a folder that are older than 14 days old. I am running into two different issues. #1 is I do not know how to get a date 14 days ago, and #2 is how do I select the files? I figure I can use the FileDelete call, once I set the date, but how do I tell which files? This is what I have: #include <Date.au3> #include <File.au3> Local $sFiles, $sPath, $sEndDate, $sStartDate ;Sets the location of the files $sPath = "c:\Downloads\" $sFiles = $sPath & '*.*' ;Sets the Start Date $sStartDate = $sEndDate - 14 ;Gets Current Date $sEndDate = _NowDate() ;Sets start date of files If ($sEndDate <= 14) Then MsgBox(0,'', "test" & $sEndDate - 14) EndIf ;Deletes files FileDelete ($sFiles & ($sEndDate) "so much work, so little brains..." Link to comment Share on other sites More sharing options...
jfcby Posted May 17, 2010 Share Posted May 17, 2010 For question #1 check this link out Find File Date and look in the help file for FileGetTimejfcby Determined -- Devoted -- Delivered Make your mind up -- to seriously apply yourself -- accomplishing the desired results. **** A soft answer turneth away wrath: but grievous words stir up anger. Proverbs 15:1 KJB **** Link to comment Share on other sites More sharing options...
zorphnog Posted May 17, 2010 Share Posted May 17, 2010 Take a look at FileGetTime, _DateDiff, FileFindFirstFile/FileFindNextFile. Link to comment Share on other sites More sharing options...
RichardT Posted May 18, 2010 Author Share Posted May 18, 2010 I am still at a loss. I can compare 2 files only, but I need to compare all of select files to do the delete based on age. How can I select them; and how do I delete them without deleting all of the files instead of the ones I need to save? Please help. "so much work, so little brains..." Link to comment Share on other sites More sharing options...
zorphnog Posted May 18, 2010 Share Posted May 18, 2010 Did you even look at the functions I referenced? Read the documentation on FileFindFirstFile and FileFindNextFile. Link to comment Share on other sites More sharing options...
jfcby Posted May 18, 2010 Share Posted May 18, 2010 Try this linkjfcby Determined -- Devoted -- Delivered Make your mind up -- to seriously apply yourself -- accomplishing the desired results. **** A soft answer turneth away wrath: but grievous words stir up anger. Proverbs 15:1 KJB **** Link to comment Share on other sites More sharing options...
jfcby Posted May 18, 2010 Share Posted May 18, 2010 (edited) Try the code below but first make sure you create a copy of the folder with the files in it that you want to delete to run a test to make sure it works as expected.This code is for 7 days...#include <File.au3> #include <Date.au3> Dim $fileList ;Root folder $sourcefolder = @ProgramFilesDir & '\FolderName' MsgBox(0, "", $sourcefolder) ;Gather files into an array ;~ $fileList = _FileListToArray($sourcefolder, "spec08_*_speclogs_PDF.log", 1) $fileList = _FileListToArray($sourcefolder, "*", 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) Nextjfcby Edited May 18, 2010 by jfcby Determined -- Devoted -- Delivered Make your mind up -- to seriously apply yourself -- accomplishing the desired results. **** A soft answer turneth away wrath: but grievous words stir up anger. Proverbs 15:1 KJB **** Link to comment Share on other sites More sharing options...
jfcby Posted May 18, 2010 Share Posted May 18, 2010 This code is for 14 days.#include <File.au3> #include <Date.au3> Dim $fileList ;Root folder $sourcefolder = @ProgramFilesDir & '\FolderName' MsgBox(0, "", $sourcefolder) ;Gather files into an array ;~ $fileList = _FileListToArray($sourcefolder, "spec08_*_speclogs_PDF.log", 1) $fileList = _FileListToArray($sourcefolder, "*", 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()) > 14 Then FileDelete($sourcefolder & "\" & $fileList[$X]) Msgbox(1, "Files deleted:", $fileList[$X], 1) Nextjfcby Determined -- Devoted -- Delivered Make your mind up -- to seriously apply yourself -- accomplishing the desired results. **** A soft answer turneth away wrath: but grievous words stir up anger. Proverbs 15:1 KJB **** Link to comment Share on other sites More sharing options...
RichardT Posted May 18, 2010 Author Share Posted May 18, 2010 jfcby, Thanks for the post to my problem. I can see where the msgbox is cycling the files, but none get deleted? Is there a switch that i need to put in to make it delete? Thanks for your all your time in nursing me along with this... "so much work, so little brains..." Link to comment Share on other sites More sharing options...
zorphnog Posted May 18, 2010 Share Posted May 18, 2010 (edited) The last If statement @jfcby wrote should be:If _DateDiff('d', $fDate,_NowCalc()) > 14 Then FileDelete($sourcefolder & "\" & $fileList[$X]) Msgbox(1, "File deleted:", $fileList[$X], 1) EndIfSee if that actually displays any files being deleted. Or alternatively, you could use this:#include <Date.au3> $iMaxDays = 14 $sFolder = "C:\Downloads" $hFindFile = FileFindFirstFile($sFolder & "\*.*") While $hFindFile <> -1 $sFilename = FileFindNextFile($hFindFile) If @error Then ExitLoop $sFile = $sFolder & "\" & $sFilename ; get full path If StringInStr(FileGetAttrib($sFile), "D") <> 0 Then ContinueLoop ; bypass folders $sFileDate = FileGetTime($sFile, 0, 1) ; get file date $sFileDate = StringRegExpReplace($sFileDate, "([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})", "$1/$2/$3 $4:$5:$6") ; convert file date format If _DateDiff("D", $sFileDate, _NowCalc()) > $iMaxDays Then FileDelete($sFile) WEnd Edited May 18, 2010 by zorphnog Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now