Jump to content
Sign in to follow this  

Delete folders that are older than 4 weeks

Recommended Posts


Hi everyone,

I'm currently trying to get my AutoIT code to delete all folders that are older than 4 weeks.

My code initially creates a folder with the computer name and then creates a folder within this that is named as the current date.

I'm trying to get the code below to delete any folders that are older than 4 weeks

$oldfolders = "c:\backup\" & @computername & "\" & @mday & "-" & @mon & "-" & @year - 4

if FileExists ($oldfolders) Then DirRemove ($oldfolders, 1)

Anyone have any ideas, thanks

Share this post

Link to post
Share on other sites

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

;Root folder
$sourceFolder = @ScriptDir & '\'

;Gather files into an array
$fileList = _FileListToArray($sourceFolder, "*", 2)
Dim $found[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('w', $fDate, _NowCalc()) > 4 Then 
        ;FileDelete($sourceFolder & "\" & $fileList[$X])
        _ArrayAdd($found, $sourceFolder & $fileList[$X])
        ;MsgBox(1, "Files deleted:", $fileList[$X], 1)


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post

Link to post
Share on other sites

Thanks. The only concern I have is that I seem to need to change the "4" to "0.5" to get it to recognize 1 week. Like the following:

If _DateDiff('w', $fDate, _NowCalc()) > 0.5 Then



If I change it to 'd' and >7 then this seems to work pretty well for my situation

Edited by jbennett

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
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.