AntiVirusGuy Posted April 9, 2006 Posted April 9, 2006 Does anyone know how to delete files in a folder keeping only the three most recent example I backup once a week, I have backup1.zip backup2.zip backup3.zip backup4.zip backup5.zip backup6.zip I want to dlete the oldest so I only keep 3 backups. The names will vary so I can not go by name
Moderators SmOke_N Posted April 9, 2006 Moderators Posted April 9, 2006 (edited) I made a function for this: http://www.autoitscript.com/forum/index.ph...stToArrayByDateAll you have to do is on the return array, is If Ubound($ReturnArray) - 1 > 3 Then For $i = 4 To Ubound($ReturnArray) - 1 FileDelete('Location\' & $ReturnArray[$i]) Next EndIfEdit:FYI: Depending on if you set it to Newest to oldest ...Edit2:Forgot to close the If/Then statement. Edited April 9, 2006 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.
Valuater Posted April 9, 2006 Posted April 9, 2006 (edited) $list = Filelisttoarray()thenfor $x = 1 to ubound($list) -1$size[$x] = filegettime()nextarraysort($size)for $x = 4 to ubound($list) -1filedelete(List[$x])nexta rought-out of a concept8)late, slow, lazy.... goodnight Edited April 9, 2006 by Valuater
AntiVirusGuy Posted April 9, 2006 Author Posted April 9, 2006 All I get is autoit errors Anybody else got any Ideas
Moderators SmOke_N Posted April 9, 2006 Moderators Posted April 9, 2006 What auotit errors are you getting? 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.
Moderators SmOke_N Posted April 9, 2006 Moderators Posted April 9, 2006 That was an example of how to set it up... You need to put in your own information (variable) on how you call the function. 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.
AntiVirusGuy Posted April 9, 2006 Author Posted April 9, 2006 (edited) Sorry to make you spell it out but I am not familiar with this one what else do I need to ad to this script if I want to delete all the files in C:\work older than 3 days? I am still getting errors If Ubound($ReturnArray) - 1 > 3 Then For $i = 4 To Ubound($ReturnArray) - 1 FileDelete('C:\work\*.*' & $ReturnArray[$i]) Next EndIf Edited April 9, 2006 by ssebrownatcolby
Moderators SmOke_N Posted April 9, 2006 Moderators Posted April 9, 2006 (edited) Sorry to make you spell it out but I am not familiar with this one what else do I need to ad to this script if I want to delete all the files in C:\work older than 3 days? I am still getting errors If Ubound($ReturnArray) - 1 > 3 Then For $i = 4 To Ubound($ReturnArray) - 1 FileDelete('C:\work\*.*' & $ReturnArray[$i]) Next EndIfDo you not know how to use functions? Or how to call them? That's not the whole thing, I gave you the link to the function you needed to call. It has an example on how to use it. Edit: Here is an example on how to use it:expandcollapse popup;Warning in it's current state, it will delete any file in that folder that is not as current as the top 3, please use the message box that is provided there to test on this, before you uncomment the FileDelete. Local $FileLocation = @DesktopDir & "\MY_FOLDER_TO_SEARCH" Local $ReturnArray = _FileListToArrayByDate($FileLocation, "AU3", @DesktopDir & "\TestFileLog.txt"); @desktop & "\testfilelog.txt" will be deleted if $i_DeleteLogFile is not changed from 1 If UBound($ReturnArray) - 1 > 3 Then For $i = 4 To UBound($ReturnArray) - 1 MsgBox(0, '', $FileLocation & $ReturnArray[$i]) ;FileDelete('Location\' & $ReturnArray[$i]) Next EndIf Func _FileListToArrayByDate($h_Directory, $s_Extension, $h_OutFile, $i_Ascending = 1, $i_DeleteLogFile = 1) If Not $h_Directory <> '' Or Not $s_Extension <> '' Or Not $h_OutFile <> '' Then SetError(0) Return 0 EndIf $v_OptRunFatal = Opt('RunErrorsFatal', 1) Local $v_RunWait = RunWait(@ComSpec & ' /c ' & 'dir ' & '"' & $h_Directory & '\*.' & $s_Extension _ & '"' & ' /b /o-e /od > ' & '"' & $h_OutFile & '"', '', @SW_HIDE) If $v_RunWait == 1 Then Opt('RunErrorsFatal', $v_OptRunFatal) SetError(1) Return 1 Else Opt('RunErrorsFatal', $v_OptRunFatal) EndIf Local $h_FileOpen = FileOpen($h_OutFile, 0) If $h_FileOpen == - 1 Then SetError(2) Return 2 EndIf Local $av_Array = StringSplit(StringTrimRight(StringStripCR(FileRead($h_FileOpen, FileGetSize($h_OutFile))), 1), @LF) FileClose($h_FileOpen) If $i_DeleteLogFile == 1 Then While FileExists($h_OutFile) Sleep(10) FileDelete($h_OutFile) WEnd ElseIf Not $i_DeleteLogFile == 0 Then SetError(5) Return 5 EndIf If Not IsArray($av_Array) Then SetError(3) Return 3 EndIf If $i_Ascending == 1 Then Local $av_TempArray = '' If UBound($av_Array) - 1 > 1 Then For $iCount = UBound($av_Array) - 1 To 1 Step - 1 $av_TempArray = $av_TempArray & $av_Array[$iCount] & Chr(01) Next $av_Array = StringSplit(StringTrimRight($av_TempArray, 1), Chr(01)) If IsArray($av_Array) Then Return $av_Array Else SetError(4) Return 4 EndIf Else Return $av_Array EndIf ElseIf $i_Ascending = 0 Then Return $av_Array Else SetError(5) Return 5 EndIf EndFunc ;==>_FileListToArrayByDate Edited April 9, 2006 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.
AntiVirusGuy Posted April 9, 2006 Author Posted April 9, 2006 I have no idea what I am doing. Sorry to be a bonehead. I can not even get the msgbox to appear. I am trying to do this in a folder C:\work which is a test dir. no important data there. I have messesd with this script for a while, it just runs no errors. anyideas? thanks for taking the time
Moderators SmOke_N Posted April 9, 2006 Moderators Posted April 9, 2006 Why don't you provide the location and the file your looking for (if it's a specific file), and I'll see if I can mod it to work for you. 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.
AntiVirusGuy Posted April 9, 2006 Author Posted April 9, 2006 for testing purposes "C:\work" would be the location and lets say "backup*.zip" for a file Thank you again for your help this is well beyond me
Moderators SmOke_N Posted April 9, 2006 Moderators Posted April 9, 2006 (edited) for testing purposes "C:\work" would be the location and lets say "backup*.zip" for a file Thank you again for your help this is well beyond meThis works:expandcollapse popupLocal $FileLocation = @HomeDrive & "\Work"; Location of file Local $FILE = '\backup*.'; File name with the '.' on the end, wildcard supported Local $ReturnArray = _FileListToArrayByDate($FileLocation, $FILE, "ZIP", @DesktopDir & "\TestFileLog.txt", 0); Calling the function - File Location / File name / Extension - Mask type / File to write to (will be deleted afterwards) If UBound($ReturnArray) - 1 > 3 Then For $x = 1 To 3; Don't need this for / next loop but you do need the 2nd one. MsgBox(0, 'Files Not Deleted', $ReturnArray[$x] & ' Will NOT be deleted') Next For $i = 4 To UBound($ReturnArray) - 1 MsgBox(0, 'Files Deleted', $FileLocation & '\' & $ReturnArray[$i] & ' Will be deleted') ;FileDelete('Location\' & $ReturnArray[$i]) Next EndIf Func _FileListToArrayByDate($h_Directory, $h_FileName, $s_Extension, $h_OutFile, $i_Ascending = 1, $i_DeleteLogFile = 1) If Not $h_Directory <> '' Or Not $s_Extension <> '' Or Not $h_OutFile <> '' Then SetError(0) Return 0 EndIf $v_OptRunFatal = Opt('RunErrorsFatal', 1) Local $v_RunWait = RunWait(@ComSpec & ' /c ' & 'dir ' & '"' & $h_Directory & $h_FileName & $s_Extension _ & '"' & ' /b /o-e /od > ' & '"' & $h_OutFile & '"', '', @SW_HIDE) If $v_RunWait == 1 Then Opt('RunErrorsFatal', $v_OptRunFatal) SetError(1) Return 1 Else Opt('RunErrorsFatal', $v_OptRunFatal) EndIf Local $h_FileOpen = FileOpen($h_OutFile, 0) If $h_FileOpen == - 1 Then SetError(2) Return 2 EndIf Local $av_Array = StringSplit(StringTrimRight(StringStripCR(FileRead($h_FileOpen, FileGetSize($h_OutFile))), 1), @LF) FileClose($h_FileOpen) If $i_DeleteLogFile == 1 Then While FileExists($h_OutFile) Sleep(10) FileDelete($h_OutFile) WEnd ElseIf Not $i_DeleteLogFile == 0 Then SetError(5) Return 5 EndIf If Not IsArray($av_Array) Then SetError(3) Return 3 EndIf If $i_Ascending == 1 Then Local $av_TempArray = '' If UBound($av_Array) - 1 > 1 Then For $iCount = UBound($av_Array) - 1 To 1 Step - 1 $av_TempArray = $av_TempArray & $av_Array[$iCount] & Chr(01) Next $av_Array = StringSplit(StringTrimRight($av_TempArray, 1), Chr(01)) If IsArray($av_Array) Then Return $av_Array Else SetError(4) Return 4 EndIf Else Return $av_Array EndIf ElseIf $i_Ascending = 0 Then Return $av_Array Else SetError(5) Return 5 EndIf EndFunc;==>_FileListToArrayByDate Edit, I commented the code Edited April 9, 2006 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.
AntiVirusGuy Posted April 10, 2006 Author Posted April 10, 2006 just seems to run .... no msgbox no log file no errors there are infact 5 zipfiles with different dates any ideas?
Moderators SmOke_N Posted April 10, 2006 Moderators Posted April 10, 2006 Well to be honest, the code worked for me... I have no idea what your doing, you've shown nothing of what your doing, or even how you replaced the location etc... You make it really hard to help you. 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.
AntiVirusGuy Posted April 11, 2006 Author Posted April 11, 2006 Sorry not trying to make it hard I have the latest autoit installed I took the script you gave me (unedited) I created a folder in the root of C called work I placed 6 files with differnt creation dates all zip files and ran the script nothing happened I did not receive any errors did not get a log and no msgbox. Let me know what other info you need. I really do thank you for your help. I have written scripts before and thought I was past begginer level...now I am wondering
Moderators SmOke_N Posted April 11, 2006 Moderators Posted April 11, 2006 Did you name the files "backup1.zip", 'backup2.zip", etc... ? 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.
AntiVirusGuy Posted April 13, 2006 Author Posted April 13, 2006 I did not modify your script at all here is a list of file in the folder I am not getting any log file or msgbox or anything visable. I have the latest autoit. Any ideas whats wrong? Directory of C:\work 04/12/2006 10:12 PM <DIR> . 04/12/2006 10:12 PM <DIR> .. 10/30/2003 03:01 PM 148,603 backup Auto_Debug_for_Windows_v2.0.1.55.zip 10/30/2003 11:42 AM 396,862 backup carbon.zip 10/30/2003 02:47 PM 486,276 backup cardscan.zip 07/22/2003 07:17 PM 176,242 backup dlgdiag10.zip 07/22/2003 07:16 PM 148,123 backup dlgudma10.zip 09/11/2004 06:33 PM 1,094,021 backup dvdshrink32setup.zip 04/09/2006 12:50 AM 56 backup New rename.zip 04/10/2006 02:13 PM 0 backup New Text Document (2).zip 04/10/2006 02:14 PM 0 backup New Text Document (3).zip 04/10/2006 02:15 PM 0 backup New Text Document (4).zip 04/10/2006 02:12 PM 0 backup New Text document.zip 04/12/2006 10:05 PM 22 backup New WinZip File.zip 02/05/2004 07:27 PM 154,757 Corel_WordPerfect_Office_v11.zip 07/22/2003 07:17 PM 39,813 dlgchk10.zip 04/12/2006 10:12 PM 0 files.txt 15 File(s) 2,644,775 bytes 2 Dir(s) 168,461,996,032 bytes free
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