Jump to content

Recommended Posts

Posted (edited)

Hi,

Currently i made a script that deletes everything older then 7 days in a script.

But for some reason when i create a folder in C:'\test and another folder IN the folder that i made. It will not get deleted by my script.

But all other extensions like .txt .rar, etc. will get deleted succesfully in the folder that i made

I uploaded a example image file for easier explaining.

this is my script

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

$Folder = "C:\test\"
$test = _FileListToArray($Folder)

If IsArray($test) Then
    For $i = 1 To UBound($test) - 1
        $tijd = StringRegExpReplace(FileGetTime($Folder & $test[$i], 1, 1), "(.{4})(.{2})(.{2})(.{2})(.{2})(.{2})", "${1}/${2}/${3} ${4}:${5}:${6}")
        If _DateDiff('D', $tijd, _NowCalc()) > 7 Then
            FileDelete($Folder & $test[$i])
            DirRemove ($Folder & $test[$i])
        EndIf
    Next
EndIf

I hope you guys can help me / point me in the right direction

Solved the problem by changing the DirRemove command!

Spoiler

example.png

 

Edited by DavidW
Posted
Quote

Remarks

Some directory attributes can make the deletion impossible, therefore if this is the case look at FileSetAttrib() to change the attributes of a directory.

Idk what attributes your folder has. Tested your script on my system and it worked (just changed the difference date to 0 so it always executed)

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

$Folder = "C:\test\"
$test = _FileListToArray($Folder)

If IsArray($test) Then
    For $i = 1 To UBound($test) - 1
        $tijd = StringRegExpReplace(FileGetTime($Folder & $test[$i], 1, 1), "(.{4})(.{2})(.{2})(.{2})(.{2})(.{2})", "${1}/${2}/${3} ${4}:${5}:${6}")
        If _DateDiff('D', $tijd, _NowCalc()) >= 0 Then
            ConsoleWrite("Removing folder " & $Folder & $test[$i] & " : " & (DirRemove($Folder & $test[$i]) ? "Succeeded" : "Failed") & @LF)
        EndIf
    Next
EndIf

 

Posted
6 minutes ago, jguinch said:

If the folder is not empty you have to force it to delete by using $DIR_REMOVE with DirRemove

Thanks problem solved!

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
  • Recently Browsing   0 members

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