#Include <Date.au3>
#Include <File.au3>
#Include <WinAPIEx.au3>
_DeleteOldFiles('C:\MyFolder', '*', 7, 1)
Func _DeleteOldFiles($sRoot, $sFile, $iDays, $fDeleteEmptyDir = 0)
Local $FileList, $hFile, $tTime1, $tTime2, $sDate1, $sDate2
$FileList = _FileListToArray($sRoot, $sFile, 1)
If Not @error Then
For $i = 1 To $FileList[0]
$hFile = _WinAPI_CreateFile($sRoot & '\' & $FileList[$i], 2, 2)
$tTime1 = _Date_Time_GetFileTime($hFile)
_WinAPI_CloseHandle($hFile)
$tTime1 = _Date_Time_FileTimeToSystemTime(DllStructGetPtr($tTime1[0]))
$sDate1 = _Date_Time_SystemTimeToDateTimeStr($tTime1, 1)
$tTime2 = _Date_Time_GetSystemTime()
$sDate2 = _Date_Time_SystemTimeToDateTimeStr($tTime2, 1)
If _DateDiff('D', $sDate1, $sDate2) >= $iDays Then
ConsoleWrite('Delete file: ' & $sRoot & '\' & $FileList[$i] & @CR)
; FileDelete($sRoot & '\' & $FileList[$i])
EndIf
$tTime1 = 0
$tTime2 = 0
Next
EndIf
$FileList = _FileListToArray($sRoot, '*', 2)
If Not @error Then
For $i = 1 To $FileList[0]
_DeleteOldFiles($sRoot & '\' & $FileList[$i], $sFile, $iDays, $fDeleteEmptyDir)
If ($fDeleteEmptyDir) And (_WinAPI_PathIsDirectoryEmpty($sRoot & '\' & $FileList[$i])) Then
ConsoleWrite('Delete folder (empty): ' & $sRoot & '\' & $FileList[$i] & @CR)
; DirRemove($sRoot & '\' & $FileList[$i])
EndIf
Next
EndIf
EndFunc ;==>_DeleteOldFiles