Sign in to follow this  
Followers 0
Zest

Copy directories and overwrite only older files

13 posts in this topic

Hello,

I would like to use dircopy in a script to copy a folder including all files and subfolders to another location, thereby only overwriting older files (to keep copying times to a minimum).The dircopy function unfortunately does not provide this option. Then I searched the forum and stumbled upon a FileCompare function to compare file attributes. So I thought of doing the copying by first getting a file list from each (sub)directory and then comparing each file to determine if the file should be copied or not. However, this seems complicated and I was hoping there is an easier way. I prefer not having to use windows shell commands like xcopy.

(Note: The source and target folders are read from an .ini file.)

Could anyone please help me out how to get this done or suggest a good approach at this?

Many thanks in advance!

Share this post


Link to post
Share on other sites



#4 ·  Posted (edited)

Something like this:

$oFile = "C:\Users\<you>\Desktop\axax.au3"
$pFile = "C:\Users\<you>\Desktop\BackUp\axax.au3"
$var1 = FileGetTime($oFile, 1, 1)
ConsoleWrite("$var1 =  " & $var1 & @CRLF)
$var2 = FileGetTime($pFile, 1, 1)
ConsoleWrite("$var2 =  " & $var2 & @CRLF)

If $var1 > $var2 Then
    FileCopy($var1, IniRead($dooda), 1)
Else
    FileCopy($var2, IniRead($dooda), 1)

    EndIf

In a For Next Loop that goes through your ini file line by line...

Edit: The top 2 files are just to test, they can be setup to be read from the ini file as well 

Edited by l3ill

Share this post


Link to post
Share on other sites

What exactly is that script snippet supposed to be doing? It doesn't look like it's complete or usable in its present state.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I haven't checked this but it should be a good start.

#include <File.au3>
#include <Array.au3>

Local $inifile = @ScriptDir & "\IniFile.ini"                        ;insert your ini file
Local $inifiledir1 = IniRead($inifile, "default", "default", "")    ;change ini section and key
Local $FileList1 = _FileListToArray($inifiledir1, -1, 1)

If @error = 4 Then
    MsgBox(0, "", "No Files Found.")
    Exit
EndIf

Local $inifiledir2 = IniRead($inifile, "default", "default2", "")   ;change ini section and key
Local $FileList2 = _FileListToArray($inifiledir2, -1, 1)

If @error = 4 Then                                                  ;No files in 2nd dir so just copy all over
    For $i = 1 To UBound($FileList1)-1
        FileCopy($inifiledir1 & $FileList1[$i], $inifiledir2 & $FileList1[$i])
    Next
    Exit
EndIf

For $i = 1 To UBound($FileList1)-1
    Local $FileDateTime1 = ""
    Local $FileDateTime2 = ""

    If FileExists($inifiledir2 & $FileList1[$i]) = 1 Then           ;if the file exists in 2nd dir then check Date Time
        $FileDateTime1 = FileGetTime($inifiledir1 & $FileList1[$i], 1, 1)

        For $o = 1 To UBound($FileList2)-1
            If $FileList1[$i] = $FileList2[$o] Then                 ;checks to make sure checking same file vs file
                $FileDateTime2 = FileGetTime($inifiledir2 & $FileList1[$o], 1, 1)
                If $FileDateTime1 > $FileDateTime2 Then             ;if newer Date Time then copy and overwrite
                    FileCopy($inifiledir1 & $FileList1[$i], $inifiledir2 & $FileList1[$i], 1)
                EndIf
            EndIf
        Next
    Else                                                            ;if doesn't exist in 2nd dir just copy it
        FileCopy($inifiledir1 & $FileList1[$i], $inifiledir2 & $FileList1[$i])
    EndIf
Next

Edit: Forgot UBound($array)-1 and added if no files are in 2nd directory just copy all to it.

Also this does NOT include subfolders!!!!!  If not using 3.3.10 check out >HERE if on 3.3.10 use _FileListToArrayRec instead of _FileListToArray

Edited by Rogue5099

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Thank you all for your help!

I'll give it a try with '_FileListToArray' and try to implement a loop to include subfolders and let you know if it works or if I need more help :-).

Edited by Zest

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Just an update and to say thank you all!:

I already have the check to determine if the .ini setting is a file or dir  working and also the generation of a file list in case of a direcetory using the _FileListToArrayRec.

Copying of files is also working and I also have a check so using system environment variables (e.g. %windir%) is also taken into account. Now all I have to do is add the check to copy only newer files, as in Rogue5099's example.

But that's a job for tomorrow... :-)

Edited by Zest

Share this post


Link to post
Share on other sites

It shouldn't be that hard to check for newer files from my example.  I was behind on updating my AutoIt version so here is my example including subfolders.

#include <File.au3>
#include <Array.au3>

Local $inifile = @ScriptDir & "\IniFile.ini"                        ;insert your ini file
Local $inifiledir1 = IniRead($inifile, "default", "default", "")    ;change ini section and key
Local $FileList1 = _FileListToArrayRec($inifiledir1, "*", 1, 1, 1, 0)

If @error = 4 Then
    MsgBox(0, "", "No Files Found.")
    Exit
EndIf

Local $inifiledir2 = IniRead($inifile, "default", "default2", "")   ;change ini section and key
Local $FileList2 = _FileListToArrayRec($inifiledir2, "*", 1, 1, 1, 0)


If @error = 4 Then                                                  ;No files in 2nd dir so just copy all over
    For $i = 1 To UBound($FileList1)-1
        FileCopy($inifiledir1 & $FileList1[$i], $inifiledir2 & $FileList1[$i])
    Next
    Exit
EndIf

For $i = 1 To UBound($FileList1)-1
    Local $FileDateTime1 = ""
    Local $FileDateTime2 = ""

    If FileExists($inifiledir2 & $FileList1[$i]) = 1 Then           ;if the file exists in 2nd dir then check Date Time
        $FileDateTime1 = FileGetTime($inifiledir1 & $FileList1[$i], 1, 1)

        For $o = 1 To UBound($FileList2)-1
            If $FileList1[$i] = $FileList2[$o] Then                 ;checks to make sure checking same file vs file
                $FileDateTime2 = FileGetTime($inifiledir2 & $FileList1[$o], 1, 1)
                If $FileDateTime1 > $FileDateTime2 Then             ;if newer Date Time then copy and overwrite
                    FileCopy($inifiledir1 & $FileList1[$i], $inifiledir2 & $FileList1[$i], 1)
                EndIf
            EndIf
        Next
    Else                                                            ;if doesn't exist in 2nd dir just copy it
        FileCopy($inifiledir1 & $FileList1[$i], $inifiledir2 & $FileList1[$i])
    EndIf
Next

Share this post


Link to post
Share on other sites

You can also look into robocopy...not an autoit solution, but a 'simple' command line executable...there are params to 'exclude older files' from the copy.


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Hello all,

Just wanted to say thank you again to all of you for your help! :thumbsup:

With your help I got everything working the way I want it :-)

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