kor Posted May 7, 2009 Share Posted May 7, 2009 #include <file.au3> #include <date.au3> ; Root folder $sourceFolder = "C:\test" ; Gather files into an array $fileList = _FileListToArray($sourceFolder, "*.*", 1) If @error Then Exit EndIf ; Loop through array For $X = 1 to $fileList[0] ; Retrieve creation time of file $Date = FileGetTime($sourceFolder & "\" & $fileList[$X], 0, 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 two weeks If _DateDiff('d', $fDate,_NowCalc()) > 14 Then FileDelete($sourceFolder & "\" & $fileList[$X]) Next Even when I change the $fileList = _FileListToArray($sourceFolder, "*.*", 1) from a 1 to a 2 for (files and folders) it's only going 1 directory deep. It will only delete files inside the C:\test folder. If there is a file inside say C:\test\1\ it will not delete any files inside that subfolder. Any help? Link to comment Share on other sites More sharing options...
Valuater Posted May 7, 2009 Share Posted May 7, 2009 You can use, modify, or take what you want from this Text/File Managerhttp://www.autoitscript.com/forum/index.ph...st&p=1413358) Link to comment Share on other sites More sharing options...
kor Posted May 7, 2009 Author Share Posted May 7, 2009 that code is way too complicated for me to really get down with it. Link to comment Share on other sites More sharing options...
Valuater Posted May 7, 2009 Share Posted May 7, 2009 I just pulled out the one that mattered, removed "my GUI" stuff and put your stuff in... ...Simple... #Include <Date.au3> search_Folders(@ScriptDir, "*.txt") Func search_Folders($Temp_Dir, $Temp_File) FileDelete(@TempDir & "\au3.txt") If StringRight($Temp_Dir, 1) <> "\" Then $Temp_Dir = $Temp_Dir & "\" RunWait(@ComSpec & ' /c ' & 'dir "' & $Temp_Dir & $Temp_File & '" /a :h /b /s' & ' > "' & @TempDir & '\au3.txt"', '', @SW_HIDE) $hFile = FileOpen(@TempDir & "\au3.txt", 0) While 1 $sLine = FileReadLine($hFile) If @error = -1 Then FileClose($hFile) Return EndIf ; Retrieve creation time of file $Date = FileGetTime($sLine, 0, 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 two weeks If _DateDiff('d', $fDate,_NowCalc()) > 14 Then MsgBox(4096, "Delete", "This file is set to be deleted..." & @CRLF & $sLine, 4) ;FileDelete($sourceFolder & "\" & $fileList[$X]) EndIf WEnd FileClose($hFile) EndFunc ;==>search_Folders 8) Link to comment Share on other sites More sharing options...
kor Posted May 7, 2009 Author Share Posted May 7, 2009 the $sourcefolder var isn't being defined anywhere. Should I define it up at "search_folders" ? Link to comment Share on other sites More sharing options...
Valuater Posted May 7, 2009 Share Posted May 7, 2009 the $sourcefolder var isn't being defined anywhere. Should I define it up at "search_folders" ?no, just use $sLine8) Link to comment Share on other sites More sharing options...
kor Posted May 7, 2009 Author Share Posted May 7, 2009 GOT IT. Thanks. Here is my final code for anyone else. #Include <Date.au3> search_Folders("C:\test", "*.*") Func search_Folders($Temp_Dir, $Temp_File) FileDelete(@TempDir & "\au3.txt") If StringRight($Temp_Dir, 1) <> "\" Then $Temp_Dir = $Temp_Dir & "\" RunWait(@ComSpec & ' /c ' & 'dir "' & $Temp_Dir & $Temp_File & '" /a :h /b /s' & ' > "' & @TempDir & '\au3.txt"', '', @SW_HIDE) $hFile = FileOpen(@TempDir & "\au3.txt", 0) While 1 $sLine = FileReadLine($hFile) If @error = -1 Then FileClose($hFile) Return EndIf ; Retrieve creation time of file $Date = FileGetTime($sLine, 0, 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 two weeks If _DateDiff('d', $fDate,_NowCalc()) > 14 Then ;MsgBox(4096, "Delete", "This file is set to be deleted..." & @CRLF & $sLine) FileDelete($sline) ;FileDelete($sourceFolder & "\" & $fileList[$X]) EndIf WEnd FileClose($hFile) EndFunc ;==>search_Folders Link to comment Share on other sites More sharing options...
Valuater Posted May 7, 2009 Share Posted May 7, 2009 Maybe change the name...and set it up as a UDF for others #include <Date.au3> _DeleteOldFiles(@ScriptDir, "*.txt") Func _DeleteOldFiles($sFolder, $sFileType = "*.*", $Age = 14) FileDelete(@TempDir & "\au3.txt") If StringRight($sFolder, 1) <> "\" Then $sFolder = $sFolder & "\" RunWait(@ComSpec & ' /c ' & 'dir "' & $sFolder & $sFileType & '" /a :h /b /s' & ' > "' & @TempDir & '\au3.txt"', '', @SW_HIDE) $hFile = FileOpen(@TempDir & "\au3.txt", 0) While 1 $sLine = FileReadLine($hFile) If @error = -1 Then FileClose($hFile) Return EndIf $Date = FileGetTime($sLine, 0, 0) $fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0], $Date[1], $Date[2], $Date[3], $Date[4], $Date[5]) If _DateDiff('d', $fDate, _NowCalc()) > $Age Then MsgBox(4096, "Delete", "This file is set to be deleted..." & @CRLF & $sLine, 4) ;FileDelete($sLine); remove comments to delete the files EndIf WEnd FileClose($hFile) EndFunc ;==>_DeleteOldFiles 8) Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted May 7, 2009 Moderators Share Posted May 7, 2009 What is the sense of having _NowCalc in the loop? Why not just delcare it in a var outside the loop? Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted May 7, 2009 Moderators Share Posted May 7, 2009 (edited) You can really take this function to the rediculous: expandcollapse popup #include <date.au3> Global $s_dir = "C:\TestDir_So_I_See_Im_Not_Deleting_Something_I_Actually_Need" Global $i_deleted_files = _File_DeleteByTime($s_dir, 14, 2, 0, 1, True) Switch $i_deleted_files Case 0 MsgBox(16, "Error", "No files were older than your choice") Case Else MsgBox(64, "Info", $i_deleted_files " were deleted.") EndSwitch ;~ $i_less_equal_greater: 0=less, 1=equal, 2=greater ;~ $i_last_created_accessed_written: 0=modified, 1= created, 2=accessed ;~ $i_file_type: 0=All file types, 1=Files only, 2=Directories only Func _File_DeleteByTime($s_directory, $i_days, $i_less_equal_greater = 0, $i_last_created_accessed_written = 0, $i_file_type = 0, $f_recurse = False) Local $s_switch = "/T:W", $s_recurse = "/s", $s_file_type = "" Switch $i_last_created_accessed_written Case 1 $s_switch = "/T:C" Case 2 $s_switch = "/T:A" EndSwitch If Not $f_recurse Then $s_recurse = "" Switch $i_file_type Case 1 $s_file_type = "-d" Case 2 $s_file_type = "d" EndSwitch Local $i_pid = Run(@ComSpec & _ " /c dir /b " & $s_recurse & " /a" & $s_file_type & " /o:d " & $s_switch & _ ' "' & $s_directory & '"', "", @SW_HIDE, 6) Local $s_files = "" While Not @error $s_files &= StdoutRead($i_pid) WEnd If Not $s_files Then Return SetError(1, 0, 0) Local $a_split = StringSplit( StringRegExpReplace($s_files, "\r|\n+\z", ""), @LF ) Local $s_now_time = _NowCalc(), $i_delete = 0, $a_date Local $s_diff, $s_format, $s_leg = "<", $i_deleted Switch $i_less_equal_greater Case 1 $s_leg = "=" Case 2 $s_leg = ">" EndSwitch For $i = $a_split[0] To 1 Step -1 $a_date = FileGetTime($a_split[$i], $i_last_created_accessed_written) If @error Then ContinueLoop $s_format = StringFormat("%s/%s/%s %s:%s:%s", _ $a_date[0], $a_date[1], $a_date[2], $a_date[3], $a_date[4], $a_date[5]) $s_diff = _DateDiff("d", $s_format, $s_now_time) If Execute($s_diff & $s_leg & $i_days) Then ;~ MsgBox(0, 0, $a_split[$i] & @CRLF & $s_diff & " is " & $s_leg & " than " & $i_days) ;~ #cs If $i_file_type = 2 Then $i_deleted += DirRemove($a_split[$i], 1) ElseIf $i_file_type = 1 Then $i_deleted += FileDelete($a_split[$i]) Else If StringInStr(FileGetAttrib($a_split[$i]), "d") Then $i_deleted += DirRemove($a_split[$i], 1) Else $i_deleted += FileDelete($a_split[$i]) EndIf EndIf ;~ #ce EndIf Next If $i_deleted Then Return $i_deleted Return SetError(1, 0, 0) EndFunc Edited May 7, 2009 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now