mrrlg Posted April 26, 2006 Share Posted April 26, 2006 I am trying to use this script to remove files older than 14 days. The script runs, checks the files and creates the log at the end of the script but does not remove any files. I'm sure it's something simple, I just can't figure it out. expandcollapse popup; Script Function: ; Remove file older than 14 days #include <date.au3> AutoItSetOption ( "RunErrorsFatal", 0) ;AutoItSetOption ( "TrayIconHide", 1) Break(0) $Hotkey = "{Pause}" HotKeySet ($Hotkey ,"StopExec") $L_Msg = "" ; init variables $Version = "0.2" $dir="C:\LGEAI\Trio" $Dircount = 0 $n_FileCount = 0 $n_FileSize = 0 $begin = TimerStart() ; _Remove_Files(root dir, inclu subdirs,remove files older than) $rc = _Remove_Files($Dir, 1,14) Exit ;=============================================================================== ; ;=============================================================================== Func _Remove_Files($T_Dir,$T_Incl_SubDir,$T_Days) Dim $n_Dirnames[200000][2]; max number of directories that can be counted Local $n_DirCount = 0 Local $n_File Local $n_Search Local $n_tFile Local $n_Fdate Writelog("### Start run ###") $T_DirCount = 1 $T_FileCount = 0 $T_FileSize = 0 ; remove the end \ If StringRight($T_Dir,1) = "\" then $T_Dir = StringTrimRight($T_Dir,1) $n_Dirnames[$T_DirCount][0] = $T_Dir $n_Dirnames[$T_DirCount][1] = 0 $T_Incl_SubDir = Int($T_Incl_SubDir) ; Exit if base dir doesn't exists If Not FileExists($T_Dir) then Return 0 ; keep on looping until all directories are counted While $T_DirCount > $n_DirCount $n_DirCount = $n_DirCount + 1 $n_Search = FileFindFirstFile($n_Dirnames[$n_DirCount][0] & "\*.*" ) If $n_Search = -1 Then ContinueLoop EndIf ShowMenu("Processing directory:" & @LF & $n_Dirnames[$n_DirCount][0],0) While 1 $n_File = FileFindNextFile($n_Search) If @error Then ExitLoop $n_tFile = $n_Dirnames[$n_DirCount][0] & "\" & $n_File ShowMenu(" File :" & $n_File,2) ; skip these references if $n_File = "." or $n_File = ".." then ContinueLoop EndIf ; if Directory than add to the list to be processed later If StringInstr(FileGetAttrib ( $n_tFile ),"D") > 0 then If $T_Incl_SubDir = 1 then $T_DirCount = $T_DirCount + 1 $n_Dirnames[$T_DirCount][0] = $n_tFile $n_Dirnames[$T_DirCount][1] = 0 EndIf Else ; Check age of the file $n_Fdate = FileGetTime($n_tFile,1) $sdate=$n_Fdate[0] & "/" & $n_Fdate[1] & "/" & $n_Fdate[2] & " " & $n_Fdate[3] & ":" & $n_Fdate[4] & ":" & $n_Fdate[5] $edate=@YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC If _DateDiff ('d',$sdate,$eDate) > $T_Days then $rc = FileRecycle($n_tFile) Writelog($sdate & " " & _DateDiff ('d',$sdate,$eDate) & " *** rc:" & $rc & " del-> " & $n_tFile) Else ; add 1 to filecount for this directory Writelog(" Keep -> " & $n_tFile) $n_Dirnames[$n_DirCount][1] = $n_Dirnames[$n_DirCount][1] + 1 EndIf EndIf Wend FileClose($n_Search) Wend Writelog("### Removing empty directories ###") For $x = $T_DirCount to 2 step -1 Writelog("Dir: " & $n_Dirnames[$x][0] & " Files:" & $n_Dirnames[$x][1]) If $n_Dirnames[$x][1] = 0 then If "user\hidden" <> StringRight($n_Dirnames[$x][0],11) then DirRemove ($n_Dirnames[$x][0]) Writelog(" Remove Dir: " & $n_Dirnames[$x][0]) EndIF EndIf Next Writelog("### End Run ###") Return ( 1 ) EndFunc ; ;====================================== ; Stop executing when Hotkey is pressed ;====================================== Func StopExec() $mrc=Msgbox(4,"Cancel Execution?","You pressed " & $Hotkey & ". Do you want to cancel the Program?") if $mrc = 6 then writelog("*** Utility Cancelled by operator.") Exit EndIf Return EndFunc ;===================================================== ; Show Splash screen or update its content ;===================================================== Func ShowMenu($g_msg,$concat) $W_Title=" file remove Utility (ver:" & $Version & ") Press " & $Hotkey & " to stop execution" $D_msg = "" If $concat > 0 Then If $concat = 1 Then $L_msg = $L_msg & @LF & $g_msg $d_msg = $L_msg Else $d_msg = $L_msg & @LF & $g_msg EndIf Else $L_msg = $g_msg $d_msg = $g_msg EndIf if WinExists($W_Title) then ControlSetText($W_Title,"","Static1",$d_msg) else SplashTextOn($W_Title,$d_msg,800,100,1,1,6,"Courier",10,600) endif Return 0 EndFunc ;============================== ; Write to logfile ;============================== Func WriteLog($msg) $msg = @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & "==>" & $msg FileWriteLine(@ScriptDir & "\RemFiles.log", $msg ) Return 0 EndFunc Link to comment Share on other sites More sharing options...
Zedna Posted April 26, 2006 Share Posted April 26, 2006 Try to use FileSetAttrib to remove hidden/system/read-only atributes before deleting. Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
mrrlg Posted April 26, 2006 Author Share Posted April 26, 2006 (edited) The files do not appear to be locked. I added this bit of code FileSetAttrib("*.txt","-R") if @error Then MsgBox (4096, "Error", "Problem setting attributes.") here in the script ; Script Function: ; Remove file older than 14 days #include <date.au3> AutoItSetOption ( "RunErrorsFatal", 0) ;AutoItSetOption ( "TrayIconHide", 1) Break(0) $Hotkey = "{Pause}" HotKeySet ($Hotkey ,"StopExec") $L_Msg = "" ; init variables $Version = "0.2" $dir="C:\LGEAI\Trio" $Dircount = 0 $n_FileCount = 0 $n_FileSize = 0 $begin = TimerStart() ; _Remove_Files(root dir, inclu subdirs,remove files older than) FileSetAttrib("*.txt","-R") if @error Then MsgBox (4096, "Error", "Problem setting attributes.") $rc = _Remove_Files($Dir, 1,14) Exit and it had not effect on the script. Text files older than 14 days are not deleted. Within the log file generated at the end of the script this message appears 2006/04/26 15:00:58 ==> Keep -> C:\LGEAI\Trio\030906Shp.txt This file 030906Shp.txt was created on 03/09/06 and should have been deleted. Edited April 26, 2006 by mrrlg Link to comment Share on other sites More sharing options...
Uten Posted April 26, 2006 Share Posted April 26, 2006 and it had not effect on the script. Text files older than 14 days are not deleted. Within the log file generated at the end of the script this message appears2006/04/26 15:00:58 ==> Keep -> C:\LGEAI\Trio\030906Shp.txt This file 030906Shp.txt was created on 03/09/06 and should have been deleted.So obviously your date check fails? (not the FileRecycle function)Add some more info to your log. Maybee somthing like this:If _DateDiff ('d',$sdate,$eDate) > $T_Days then $rc = FileRecycle($n_tFile) Writelog($sdate & " " & _DateDiff ('d',$sdate,$eDate) & " *** rc:" & $rc & " del-> " & $n_tFile) Else ; add 1 to filecount for this directory Writelog(" Keep -> " & $n_tFile & '>>> _DateDiff (\'d\',' & $sdate & ', ' & $eDate & '):=' & _DateDiff ('d',$sdate,$eDate)) $n_Dirnames[$n_DirCount][1] = $n_Dirnames[$n_DirCount][1] + 1 EndIfI allso think @Larry's tip is wery good as it will remove locked files on next boot. Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling Link to comment Share on other sites More sharing options...
mrrlg Posted April 26, 2006 Author Share Posted April 26, 2006 I will try your script and see if I can make it work. I can't use Larry's suggestion because the finished script will run against a production machine that needs to be up 24/7. If I can get the date check function to work, then the alternative would be simply to move all files older than 14 days to another folder. Link to comment Share on other sites More sharing options...
mrrlg Posted April 26, 2006 Author Share Posted April 26, 2006 I edited the writelog statement in my script to include your suggestion and the script errors at this line: Writelog(" Keep -> " & $n_tFile & '>>> _DateDiff (\'d\',' & $sdate & ', ' & $eDate & '):=' & _DateDiff ('d',$sdate,$eDate)) I receive a "Unable to parse line" at (\'d\ Link to comment Share on other sites More sharing options...
Uten Posted April 27, 2006 Share Posted April 27, 2006 (edited) I edited the writelog statement in my script to include your suggestion and the script errors at this line:Writelog(" Keep -> " & $n_tFile & '>>> _DateDiff (\'d\',' & $sdate & ', ' & $eDate & '):=' & _DateDiff ('d',$sdate,$eDate))I receive a "Unable to parse line" at (\'d\Uh, and you did not figure out how to deal with it? (EDIT: This came out a bit rude, that was not the intention, I'm just a bit pussled.)So, replace \'d\' with d or anything, I did not run this code. I only sugested that you tried to add some relevant information in your log. Edited April 27, 2006 by Uten Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling 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