Sign in to follow this  
Followers 0
AntiVirusGuy

Delete the the 3 oldest backups or files

1 post in this topic

#1 ·  Posted (edited)

SmOke n wrote this script a long time ago we finally made it work via Pm so I thought I should share it with the world :)

Local $FileLocation = @HomeDrive & "\Work"; Location of file
Local $FILE = '\backup*.'; File name with the '.' on the end, wildcard supported
If FileExists($FileLocation) Then
    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($FileLocation & '\' & $ReturnArray[$i])
        Next
    EndIf
Else
    MsgBox(0, 'Error', '$FileLocation does not contain the actual file location.')
EndIf

Func _FileListToArrayByDate($h_Directory, $h_FileName, $s_Extension, $h_OutFile, $i_Ascending = 1, $i_DeleteLogFile = 1)
    If $h_Directory = '' Or $s_Extension = '' Or $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
Edited by ssebrownatcolby

Share this post


Link to post
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
Sign in to follow this  
Followers 0