Jump to content

Get latest files for backup


Recommended Posts

This is supposed to copy all of the files that were added/modified after the specified date to the specified folder. It updates the ones that were changed but doesn't add the new ones. It also copies files that were in subfolders in the origin folder to the main backup directory instead of to the corresponding subfolders. Any help is appreciated :) .

Func _Latest($dir, $date, $dest)
    Local $find
    Local $count = 0
    $find = FileFindFirstFile($dir & "\*.*")
    $findz = FileFindFirstFile($dir & "\*")
    Do
        $zfile = FileFindNextFile($find)
        $zfolder = FileFindNextFile($findz)
        $timefol = FileGetTime($zfolder, 0, 1)
        If FileGetTime($zfolder, 1, 1) > $timefol Then ; this checks if the date it was added is more recent than the date modified
            $timefol = FileGetTime($zfolder, 1, 1)
        EndIf
        $timefil = FileGetTime($zfile, 0, 1)
        If FileGetTime($zfile, 1, 1) > $timefil Then; this checks if the date it was added is more recent than the date modified
            $timefil = FileGetTime($zfile, 1, 1)
        EndIf
        If $timefil > $date Then
            FileCopy($zfile, $dest, 1)
        EndIf
        If $timefol > $date Then
            FileCopy($zfolder, $dest, 1)
        EndIf
        $count = $count + 1
    Until $count = 100
EndFunc
Link to comment
Share on other sites

This seems to make the assumption that objects without extensions are folders:

$find = FileFindFirstFile($dir & "\*.*")
    $findz = FileFindFirstFile($dir & "\*")

That is not a safe assumption. Files without extensions and directories that have extensions are common.

Haven't worked through if that has anything to do with your issue...

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I don't think that it has anything to do with the issue.

Haven't worked through if that has anything to do with your issue...

Link to comment
Share on other sites

Doing DirCopy() with flag = 0 (do not overwrite) will get all the new files in one stroke and put them in the proper subfolders.

To make your target folder for the FileCopy()'s include the subfolder, you'll need to extract the base file location from the source path and append it to the destination path. That would look something like this (untested):

Func _Latest($dir, $date, $dest)
    ; Copy any new files first
    DirCopy($dir, $dest, 0) ; 0 = no overwrite
    
    ; Find newer exixting files
    Local $find
    Local $count = 0
    $find = FileFindFirstFile($dir & "\*.*")
    $findz = FileFindFirstFile($dir & "\*")
    Do
        $zfile = FileFindNextFile($find)
        $zfolder = FileFindNextFile($findz)
        $timefol = FileGetTime($zfolder, 0, 1)
        If FileGetTime($zfolder, 1, 1) > $timefol Then ; check if date added is later than modified
            $timefol = FileGetTime($zfolder, 1, 1)
        EndIf
        $timefil = FileGetTime($zfile, 0, 1)
        If FileGetTime($zfile, 1, 1) > $timefil Then ; check if date added is later than modified
            $timefil = FileGetTime($zfile, 1, 1)
        EndIf
        If $timefil > $date Then
            ; get the subfolder porttion of the path
            $subfol = StringReplace($zfile, $dir, "")
            
            ; copy
            FileCopy($zfile, $dest & $subfol, 1)
        EndIf
        If $timefol > $date Then
            ; get the subfolder porttion of the path
            $subfol = StringReplace($zfolder, $dir, "")
            
            ; copy 
            FileCopy($zfolder, $dest & $subfol, 1)
        EndIf
        $count = $count + 1
    Until $count = 100
EndFunc   ;==>_Latest

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks, but unfortunately the edited function paired with the following still doesn't work on new files (the original didn't work either). The DirCopy returns 0.

If MsgBox(4, "Backup Latest", "Would you like to backup the files added/modified since the last backup?") = 6 Then
            $date = IniRead($installationdir & "\Backup Settings\Settings.ini", "Latest", "Date", 0)
            $dir = FileSelectFolder("Locate the folder that the recently modified files you want to backup are from.", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")
            $dest = FileSelectFolder("Locate the folder that you want to put the recently modified files in.", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", 2)
            MsgBox(0, "Backup Latest", "The backup shall now begin.")
            _Latest($dir, $date, $dest)
            MsgBox(0, "Backup", "The recently modified files have been backed up.")
            IniWrite($installationdir & "\Backup Settings\Settings.ini", "Latest", "Date", @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC)
        EndIf
oÝ÷ Ú«¨µéÚ

:)

Link to comment
Share on other sites

Thanks, but unfortunately the edited function paired with the following still doesn't work on new files (the original didn't work either). The DirCopy returns 0.

Get a look at what is being passed to the function by adding this to the debug message:

If MsgBox(4, "Backup Latest", "Would you like to backup ...?") = 6 Then
    $date = IniRead($installationdir & "\Backup Settings\Settings.ini", "Latest", "Date", 0)
    $dir = FileSelectFolder("Locate the folder ...", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")
    $dest = FileSelectFolder("Locate the folder ...", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", 2)
    
    MsgBox(0, "Backup Latest", "The backup shall now begin:" & @CRLF & _
            "$dir = " & $dir & @CRLF & _
            "$date = " & $date & @CRLF & _
            "$dest = " & $dest)
    
    _Latest ($dir, $date, $dest)
    
    MsgBox(0, "Backup", "The recently modified files have been backed up.")
    IniWrite($installationdir & "\Backup Settings\Settings.ini", "Latest", "Date", @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC)
EndIf

Post what those three variables come up as, so I can try to duplicate the symptoms with similar inputs.

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

$date = 20070526075858

$dest = C:\F Backup

$dir = F:\

Post what those three variables come up as, so I can try to duplicate the symptoms with similar inputs.

Link to comment
Share on other sites

OK, this works for me. Does not copy empty directories regardless of their timestamp, but will recurse through all files and subfolders of $dir, looking for any files newer than $date, and copies them to the correct subfolders of $dest. Added error reporting too.

Enjoy:

$sDate = "20070526075858"
$sDir = "C:\Temp\Temp1"
$sDest = "C:\Temp\Temp2"

$Result = _DirCopySince($sDir, $sDate, $sDest)

MsgBox(0, "Result", "The recently modified files have been backed up." & @CRLF & _
        "Returned count = " & $Result & @CRLF & _
        "@error = " & @error & @CRLF & _
        "@extended = " & @extended)


; --------------------------------------------------------
; Function _DirCopySince()
;   Copies subdirectories and files created or modified since a given timestamp.
;   Call with:  _DirCopySince($dir, $date, $dest)
;   Where:  $dir = source directory
;           $date = timestamp to use, as returned from FileGetTime() with format option 1.
;           $dest = destination directory in which everything will be copied.
;   On success returns the count of copied objects (directories and files) and @error = 0
;   On failure sets @error, and returns count of objects copied before error occured.
;       On @error, @extended = 1 if error occured during recursion into a subdirectory
; --------------------------------------------------------
Func _DirCopySince($dir, $date, $dest)
    Local $count = 0, $zObject, $TimeStamp, $RecurseDest, $RecurseRet
    
    $dir = StringStripWS($dir, 3)
    If StringRight($dir, 1) <> "\" Then $dir = $dir & "\"
    If Not FileExists($dir) Then Return SetError(1, 0, $count)
    
    $dest = StringStripWS($dest, 3)
    If StringRight($dest, 1) <> "\" Then $dest = $dest & "\"
    
    Local $find = FileFindFirstFile($dir & "*.*")

    While 1
        $zObject = FileFindNextFile($find)
        If @error Then
            ExitLoop
        Else
            $zObject = $dir & $zObject
        EndIf
        
        If StringInStr(FileGetAttrib($zObject), "D") Then
            ; Directory - Recurse with the subdirectory
            $RecurseDest = $dest & StringReplace($zObject, $dir, "")
            $RecurseRet = _DirCopySince($zObject, $date, $RecurseDest)
            If @error Then
                Return SetError(@error, 1, $count) ; @extended = 1 for during recursion
            Else
                $count += $RecurseRet + 1
            EndIf
        Else
            ; File
            $TimeStamp = FileGetTime($zObject, 0, 1)
            If FileGetTime($zObject, 1, 1) > $TimeStamp Then ; check if date added is later than modified
                $TimeStamp = FileGetTime($zObject, 1, 1)
            EndIf
            
            If $TimeStamp > $date Then
                ; copy file
                If FileCopy($zObject, $dest, 8 + 1) Then
                    $count += 1
                Else
                    Return SetError(3, 0, $count)
                EndIf
            EndIf
        EndIf
    WEnd
    
    ; Return count
    Return $count
EndFunc   ;==>_DirCopySince

:)

P.S. Notice the use of FileGetAttrib() to detect directories, NOT the absence of an extension...

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Wow! Thanks!

OK, this works for me. Does not copy empty directories regardless of their timestamp, but will recurse through all files and subfolders of $dir, looking for any files newer than $date, and copies them to the correct subfolders of $dest. Added error reporting too.

Link to comment
Share on other sites

I just tried your function on a drive-to-folder and it gave an error message that said "Unable to open file, the maximum number of open files has been exceeded." It was line 22 of the function. This is something wrong with another part of the program, right? Can you tell me what it might be?

You're welcome. It was good practice with recursion.

:)

Link to comment
Share on other sites

I just tried your function on a drive-to-folder and it gave an error message that said "Unable to open file, the maximum number of open files has been exceeded." It was line 22 of the function. This is something wrong with another part of the program, right? Can you tell me what it might be?

If you copied it verbatim from post #8, line 22 is a comment line. Post your version and I'll take a look.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Oops, I meant line 34. It's the "Local $find = FileFindFirstFile($dir & "*.*")" line.

If you copied it verbatim from post #8, line 22 is a comment line. Post your version and I'll take a look.

:)

Link to comment
Share on other sites

Oops, I meant line 34. It's the "Local $find = FileFindFirstFile($dir & "*.*")" line.

Hmm... :)

I may have fallen afoul of this from the help file for FileFindFirstFile():

When you have finished searching with the FileFind... functions you must call FileClose() to release the search handle.

The recursion may have gone nuts creating handles that never get closed.

Try this mod -- insert the following just before every instance of "Return" in the function, after the handle is created:

FileClose($find)oÝ÷ Ù8Z·
.ÖÞjÙbë8÷®ZÞöN«zË¥¶+ºÚ"µÍÌÍÜÑ]HH    ][ÝÌ
Ì
L
ÍN
N   ][ÝÂÌÍÜÑH ][ÝÐÎÌLÕ[  ÌLÕ[I][ÝÂÌÍÜÑÝH    ][ÝÐÎÌLÕ[  ÌLÕ[][ÝÂÌÍÔÝ[HÑÛÜTÚ[ÙJ ÌÍÜÑ    ÌÍÜÑ]K  ÌÍÜÑÝ
BÙÐÞ
    ][ÝÔÝ[   ][ÝË  ][ÝÕHXÙ[H[ÙYYY[È]HY[XÚÙY][ÝÈ   [ÈÔ   [ÈÂBI][ÝÔ]YÛÝ[H   ][ÝÈ  [È ÌÍÔÝ[   [ÈÔ   [ÈÂBI][ÝÐÜH    ][ÝÈ  [ÈÜ   [ÈÔ   [ÈÂBI][ÝÐ^[YH   ][ÝÈ  [È^[Y
BÈKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKBÈ[Ý[ÛÑÛÜTÚ[ÙJ
BÈÛÜYÈÝXXÝÜYÈ[[ÈÜX]YÜ[ÙYYYÚ[ÙHHÚ][[YÝ[ÈØ[Ú]ÑÛÜTÚ[ÙJ ÌÍÙ  ÌÍÙ]K    ÌÍÙÝ
BÈÚN  ÌÍÙHÛÝÙHXÝÜBÈ  ÌÍÙ]HH[YÝ[ÈÙKÈ]YÛH[QÙ][YJ
HÚ]ÜX]Ü[ÛKÈ    ÌÍÙÝHÝ[][ÛXÝÜH[ÚXÚ]][ÈÚ[HÛÜYYÈÛÝXØÙÜÈ]ÈHÛÝ[ÙÛÜYYØXÝÈ
XÝÜYÈ[[ÊH[ÜHÈÛZ[HÙ]ÈÜ[]ÈÛÝ[ÙØXÝÈÛÜYYYÜHÜØØÝYÈÛÜ^[YHHYÜØØÝY[ÈXÝÚ[Û[ÈHÝXXÝÜBÈKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKB[ÈÑÛÜTÚ[ÙJ ÌÍÙ  ÌÍÙ]K    ÌÍÙÝ
BSØØ[ ÌÍØÛÝ[H    ÌÍÞØXÝ ÌÍÕ[YTÝ[    ÌÍÔXÝÙQÝ  ÌÍÔXÝÙT]IÌÍÙHÝ[ÔÝÔÊ    ÌÍÙÊBRYÝ[ÔYÚ
    ÌÍÙJH    ÉÝÈ  ][ÝÉÌLÉ][ÝÈ[  ÌÍÙH ÌÍÙ  [È ][ÝÉÌLÉ][ÝÂRYÝ[Q^ÝÊ    ÌÍÙH[]Ù]ÜK ÌÍØÛÝ[
BIÌÍÙÝHÝ[ÔÝÔÊ  ÌÍÙÝÊBRYÝ[ÔYÚ
    ÌÍÙÝJH  ÉÝÈ  ][ÝÉÌLÉ][ÝÈ[  ÌÍÙÝH   ÌÍÙÝ    [È ][ÝÉÌLÉ][ÝÂSØØ[ ÌÍÙ[H[Q[Ý[J ÌÍÙ  [È ][ÝÊ][ÝÊBUÚ[HBBIÌÍÞØXÝH[Q[^[J ÌÍÙ[
BBRYÜ[BBQ^]ÛÜBQ[ÙBBBIÌÍÞØXÝH   ÌÍÙ  [È ÌÍÞØXÝBQ[YBRYÝ[Ò[Ý[QÙ]]X   ÌÍÞØXÝ
K   ][ÝÑ  ][ÝÊH[BBNÈXÝÜHHXÝÙHÚ]HÝXXÝÜBBBIÌÍÔXÝÙQÝH   ÌÍÙÝ    [ÈÝ[ÔXÙJ    ÌÍÞØXÝ ÌÍÙ  ][ÝÉ][ÝÊBBBIÌÍÔXÝÙT]HÑÛÜTÚ[ÙJ ÌÍÞØXÝ ÌÍÙ]K    ÌÍÔXÝÙQÝ
BBBRYÜ[BBBQ[PÛÜÙJ   ÌÍÙ[
BBBBT]Ù]ÜÜK  ÌÍØÛÝ[
HÈ^[YHHÜ[ÈXÝÚ[ÛBBQ[ÙBBBBIÌÍØÛÝ[
ÏH ÌÍÔXÝÙT]
ÈBBBQ[YBQ[ÙBBBNÈ[BBBIÌÍÕ[YTÝ[H[QÙ][YJ   ÌÍÞØXÝJBBBRY[QÙ][YJ   ÌÍÞØXÝKJH  ÝÈ    ÌÍÕ[YTÝ[[ÈÚXÚÈY]HYYÈ][[ÙYYYBBBIÌÍÕ[YTÝ[H[QÙ][YJ  ÌÍÞØXÝKJBBBQ[YBBRY ÌÍÕ[YTÝ[    ÝÈ    ÌÍÙ]H[BBBNÈÛÜH[BBBBRY[PÛÜJ  ÌÍÞØXÝ ÌÍÙÝ
ÈJH[BBBBIÌÍØÛÝ[
ÏHBBBBQ[ÙBBBBBQ[PÛÜÙJ  ÌÍÙ[
BBBBBT]Ù]ÜË  ÌÍØÛÝ[
BBBBQ[YBBQ[YBQ[YUÑ[NÈ]ÛÝ[Q[PÛÜÙJ ÌÍÙ[
BT] ÌÍØÛÝ[[[ÈÏOIÝ×ÑÛÜTÚ[Ù

Hope that helps!

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks again! It works perfectly!

Thank you for the testing. I never saw that error and wouldn't have caught it for debug.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...