Jump to content

File Delete


Recommended Posts

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

For question #1 check this link out Find File Date and look in the help file for FileGetTime

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

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

Try this link

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

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)
Next

jfcby

Edited 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

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)
Next

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

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

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)
EndIf

See 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 by zorphnog
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...