Jump to content

Requesting some assistance with datediff and newest folders.


Go to solution Solved by Nine,

Recommended Posts

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.

#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

  • Solution

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 by Nine
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...