Delete folder by date
AutoIT Code Sample - Delete folder by date
AutoIT Code Sample - Delete subfolders older than a given number of weeks
Imagine this scenario...on a particular Windows server, a faulty application is creating temporary folders and not deleting them.
This AutoIT script can be used to delete all of the subfolders that are older than a given number of weeks. The folder age is determined by the creation date of the folder itself, it does not look at any of the files inside; or the last accessed date! An event is then added to the server's event log to list the number of folders deleted as well as the space saved (in MB).Here is the source code:
;===============================================================================
; Description: Delete folder by date
; Author(s): Videre Research, LLC - http://videreresearch.com
;===============================================================================
#Include <file.au3></file.au3>
#Include <array.au3></array.au3>
#Include <date.au3></date.au3>
;The path of the folder containing the temporary folders to be deleted
$TempPath = "C:\Program Files\temp\dmserver\tmp"
;Files older than this given number of weeks will be deleted
$numberOfWeeks = 3
;The size of the folder before the delete
$sizeBefore = Round(DirGetSize($TempPath) / 1024 / 1024)
$FileList=_FileListToArray($TempPath, "*", 2)
If @Error=1 Then
;MsgBox (0,"","No Files\Folders Found.")
Exit
EndIf
;_ArrayDisplay($FileList,"$FileList")
$NUmberOfFiles = UBound($FileList)
ToolTip($NUmberOfFiles & "Files ")
$deletedFolders = 0
for $i = 1 To $NUmberOfFiles - 1
ToolTip(ProcessGetStats() & " - File " & $i & " of " & $NUmberOfFiles & " - " & $FileList[$i])
if FileExists($TempPath & "\" & $FileList[$i]) Then
$t = FileGetTime($TempPath & "\" & $FileList[$i], 1)
If Not @error Then
$yyyymd = $t[0] & "/" & $t[1] & "/" & $t[2]
ConsoleWrite($TempPath & "\" & $FileList[$i] & " - " & $yyyymd)
if _DateDiff( 'w', $yyyymd, _NowCalc()) > $numberOfWeeks Then
ConsoleWrite(" old" & @CRLF)
$deletedFolders += 1
DirRemove($TempPath & "\" & $FileList[$i], 1)
Else
ConsoleWrite(@CRLF)
EndIf
EndIf
EndIf
Next
$sizeAfter = Round(DirGetSize($TempPath) / 1024 / 1024)
$size = $sizeBefore - $sizeAfter
;Add an event to the event log
$SystemEvent='eventcreate /T WARNING /ID 999 /L APPLICATION /SO "File Cleanup" /D "' & $deletedFolders & ' old temp folders ('& $size &' MB) were deleted in folder ' & $TempPath & '."'
RunWait(@ComSpec & " /c " & $SystemEvent, "", @SW_HIDE)
Sleep(10000)
Now all you have to do is schedule this task daily or weekly, and the files will be under control...
Posted Comments: 0