DavidW

Deleting files older then XX days

3 posts in this topic

#1 ·  Posted (edited)

Hi guys,

I want to make a script where i delete files from a folder older then 7 days,

I have googled and tried to make a script, but it doesn't seem to work.

I would love to hear your improvements/ tips/ suggestions/hints  to make it work

C:\test is a old  folder i created with some files which i want to delete

#include <File.au3>
#include <Date.au3>

$test = _FileListToArray("C:\test")
$date=@YEAR&"/"&@MON&"/"&@MDAY
$date2= _DateAdd("D", -7, $date)
$date3=StringSplit($date2,"")
$date4= $date3[1]&$date3[2]&$date3[3]&@HOUR&@MIN&@SEC

if IsArray($test) Then
for $i = 1 to UBound($test)  - 1
    $tijd = FileGettime("C:\test" & $test [$i],0, 0)
If $tijd  < $date Then
    FileDelete($test[$i])
EndIf
Next
EndIf

 

Edited by DavidW

Share this post


Link to post
Share on other sites



You could try this approach.

#include <File.au3>
#include <Date.au3>

Local $Folder = @ScriptDir & "\" ; "C:\test\" ;
$test = _FileListToArray($Folder)

If IsArray($test) Then
    For $i = 1 To UBound($test) - 1
        $tijd = StringRegExpReplace(FileGetTime($Folder & $test[$i], 0, 1), "(.{4})(.{2})(.{2})(.{2})(.{2})(.{2})", "${1}/${2}/${3} ${4}:${5}:${6}") ; Last modified Date
        If _DateDiff('D', $tijd, _NowCalc()) > 7 Then ; 'D' = Difference in days between the given dates
            ;FileDelete($Folder & $test[$i])
            ConsoleWrite("Last modified Date " & $tijd & "  Delete: " & $Folder & $test[$i] & @CRLF)
        EndIf
    Next
EndIf

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Just now, Malkey said:

You could try this approach.

#include <File.au3>
#include <Date.au3>

Local $Folder = @ScriptDir & "\" ; "C:\test\" ;
$test = _FileListToArray($Folder)

If IsArray($test) Then
    For $i = 1 To UBound($test) - 1
        $tijd = StringRegExpReplace(FileGetTime($Folder & $test[$i], 0, 1), "(.{4})(.{2})(.{2})(.{2})(.{2})(.{2})", "${1}/${2}/${3} ${4}:${5}:${6}") ; Last modified Date
        If _DateDiff('D', $tijd, _NowCalc()) > 7 Then ; 'D' = Difference in days between the given dates
            ;FileDelete($Folder & $test[$i])
            ConsoleWrite("Last modified Date " & $tijd & "  Delete: " & $Folder & $test[$i] & @CRLF)
        EndIf
    Next
EndIf

 

nvm solved my own question thanks for help!

Edited by DavidW

Share this post


Link to post
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