Puts-The-New-In-Noobie Posted April 12, 2022 Share Posted April 12, 2022 Hello to all that read this post. I am currently writing an Autoit script to backup files and folders. Much more creative power with Autoit vs writing 'batch' files My goal is to have the script read from a backup folder, delete all but the newest 3 backup folders. This way if something goes wrong , the script won't delete 'all' of the backup folders. I found the code below on the forums. It woks 'fantastic'. Though it deletes all the newest files, and keeps the oldest. An exact opposite to what I am trying to achieve. I tried switching 'Global $iOldestTime = 99999999999999; YYYYMMDDhhmmss' to ';Global $iOldestTime = 11111111111111; YYYYMMDDhhmmss'. That had no effect. Also tried switching 'If $iFileTime > $iOldestTime Then' to 'If $iFileTime < $iOldestTime Then'. That also had no effect I switched them both thinking a simple switch would change the the sort order. The one thing I did do (and proud of) is I was able to do 'For $n = 4 To $avFiles[0]'. The '4' subtracts 3 form the total. So 3 folders are always left alone. Anyone have any pointers on how to switch the sort order form 'oldest' to newest' ? #include <File.au3> #include <Array.au3> ; Only required to display the arrays #include <MsgBoxConstants.au3> #include <Date.au3> ;$avFiles = _FileListToArray("C:\temp123\S10098", "*PNBCD001*") $avFiles = _FileListToArray("H:\backup0") Global $iOldestTime = 99999999999999; YYYYMMDDhhmmss ;Global $iOldestTime = 11111111111111; YYYYMMDDhhmmss Global $iOldestIndex = 0; Array index of oldest file ; Find the oldest file ;For $n = 1 To $avFiles[0] For $n = 1 To $avFiles[0] $iFileTime = Number(FileGetTime($avFiles[$n], 0, 1)) ;If $iFileTime < $iOldestTime Then If $iFileTime < $iOldestTime Then $iOldestTime = $iFileTime $iOldestIndex = $n EndIf Next _ArrayDisplay($avFiles) ; Delete all but the oldest file If $iOldestIndex > 0 Then ;For $n = 4 To $avFiles[0] For $n = 0 To $avFiles[0] ;If $n <> $iOldestIndex Then FileDelete($avFiles[$n]) If $n <> $iOldestIndex Then ConsoleWrite('Deleted file = ' & $avFiles[$n] & @LF) Next Else MsgBox(16, "Error", "Failed to find an newsest file.") endif Backup script I have written so far. expandcollapse popup#Include <File.au3> #Include <Array.au3> #include <MsgBoxConstants.au3> ; Create time stamp Local $tstampSec = @YEAR & '-' & @MON & '-' & @MDAY & '-' & @HOUR & '-' & @MIN & '-' & @SEC Local $tstampMin = @YEAR & '-' & @MON & '-' & @MDAY & '-' & @HOUR & '-' & @MIN ; ================== create log folder and backup folder ====================== $FolderForLogFiles = 'h:\Backup1-Logfiles\' ; Folder to put put logfiles $BackupFileName = "FileName-" $MainLogNameWithTimeStamp = ($BackupFileName & $tstampSec) DirCreate ($FolderForLogFiles) If FileExists ($FolderForLogFiles) Then _FileWriteLog($FolderForLogFiles & $MainLogNameWithTimeStamp & "-Krap.txt" , "Logfile Folder Exsits = Yes") Else _FileWriteLog($FolderForLogFiles & $MainLogNameWithTimeStamp & "-Krap.txt" , "Logfile Folder Exsits = No") EndIf ; -------------------------------- create backup folder to work with -------------------------------- $MainBackupFolderName = $MainLogNameWithTimeStamp ; $FolderForBackups = 'h:\backup0\' DirCreate ($FolderForBackups & $MainBackupFolderName) If FileExists ($FolderForBackups & $MainBackupFolderName) Then _FileWriteLog($FolderForLogFiles & $MainBackupFolderName & "-Krap.txt" , "Backup Folder Exsits = Yes") Else _FileWriteLog($FolderForLogFiles & $MainBackupFolderName & "-Krap.txt" , "Backup Folder Exsits = No") EndIf ; ================== create log folder and backup folder ====================== ; ================== ROBOCOPY ====================== $FolderSource01 = "G:\Temp_Q\Mikes-Tools\" $FolderDestination01 = ($FolderForBackups & $MainBackupFolderName) Local $param00 = (@ComSpec & " /k") Local $param01 = "robocopy" Local $param05 = " " Local $param10 = $FolderSource01 Local $param20 = $FolderDestination01 Local $param25 = " /e /fft" Local $param30= "/MT:12" Local $sRun = $param00&$param05&$param01&$param05&$param10&$param05&$param20&$param05&$param25&$param05&$param30 ConsoleWrite(' $sRun >'&$sRun&'<'&@CRLF) ; ..this way you can see what is gonna execute ;RunWait($sRun, "", @SW_MAXIMIZE) ; ================== ROBOCOPY ====================== ; ================== REGEDIT MPC ====================== ;RegeditMPC() Func RegeditMPC() $BackupFolderToCopyRegFileInto =$MainBackupFolderName $ProgramToRun01 = "regedit" $Arguments01 = "/e /y" $NameOfRegFile01 = "\MPC-Favorites.reg" $RegKeyToExport01 = "HKEY_CURRENT_USER\Software\MPC-HC\MPC-HC\Favorites\Files" Local $param00 = (@ComSpec & " /k") Local $param01 = $ProgramToRun01 Local $param05 = " " Local $param07 = $Arguments01 Local $param08 = $BackupFolderToCopyRegFileInto Local $param10 = $NameOfRegFile01 Local $param20 = $RegKeyToExport01 Local $sRun = $param00&$param05&$param01&$param05&$param07&$param05&$param08&$param10&$param05&$param20 ConsoleWrite(' $sRun >'&$sRun&'<'&@CRLF) ; ..this way you can see what is gonna execute RunWait($sRun, "", @SW_MAXIMIZE) EndFunc ; ================== REGEDIT MPC ====================== Link to comment Share on other sites More sharing options...
Solution Nine Posted April 12, 2022 Solution Share Posted April 12, 2022 (edited) Try this: #include <File.au3> #include <Array.au3> $avFiles = _FileListToArray("Your path here", Default, $FLTA_FOLDERS, True) _ArrayColInsert($avFiles, 1) For $i = 1 To $avFiles[0][0] $avFiles[$i][1] = FileGetTime($avFiles[$i][0], $FT_CREATED, $FT_STRING) Next _ArraySort($avFiles, 1, 1, Default, 1) _ArrayDisplay($avFiles) For $i = 4 to $avFiles[0][0] ConsoleWrite($avFiles[$i][0] & @CRLF) ; FileDelete($avFiles[$i][0]) Next Edited April 12, 2022 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Puts-The-New-In-Noobie Posted April 12, 2022 Author Share Posted April 12, 2022 @Nine Thank you It works perfectly. I also have a folder that I keep backup images of my 'c' drive. I will adept this to working with files. Again thank you so much. 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