Sign in to follow this  
Followers 0
Apzo

Synchronize the content of 2 directories

19 posts in this topic

#1 ·  Posted (edited)

Hello !

I searched a way to easily backup my au3 scripts from work to home and vice versa.

I found no (simple) equivalent to rsync on windows.

So this is my attempt.

It works "as is" and lets you choose friendly the source and destination directories, or with command line args.

In case of command line mode, remember to enclose spaced paths with "

Example : rsync "c:\documents and settings\me\my_folder" e:\

where e:\ is your USB storage key for example :)

Rsync checks if a newer version of a file exists on both sides, and copy the newest at the right place.

Rsync doesn't keep an history of your files as SVN would, so in case a file has been modified in the source AND destination, the newest will stay and the older will be overwritten.

I hope this will help :whistle:

Apzo.

[EDIT] Script updated, thanks to DMEE :)

rsync.au3

Edited by Apzo

Share this post


Link to post
Share on other sites



Or you could start a new briefcase. I guess with this you don't have to drag and drop each file. :whistle:

Share this post


Link to post
Share on other sites

No drag and drop needed, just select the 2 folders to synchonize and it's done :)

Usefull when you work on a project at home and at work : just rsync your project on an USB key.

I'm working on a net version using FTP, so you can allways get the latests files up to date anywhere.

I'm thinking about a file history system, too.

You would be able to set the number of backups for your files, and retrieve up to version-X if needed.

No extra soft to install, just a standalone .exe : AU3 rocks :whistle:

Apzo.

Share this post


Link to post
Share on other sites

Hey Apzo, this is awesome. I work on my scripts at home and at work and it has always been a pain to figure out which ones are up to date. I'll start using this! thanks a bunch!

Share this post


Link to post
Share on other sites

Very useful script.

Thanks for sharing.

/ K :)

Share this post


Link to post
Share on other sites

The script is really useful, but one small bug fix in your script would be changing:

If StringRight($PathTo, 1) = "\" Then
    $PathFrom = StringTrimRight($PathTo, 1)
EndIf

In the beginning there was nothing and then even that exploded - anonymous

Share this post


Link to post
Share on other sites

There is Microsoft's SyncToy. It works rather well for syncing files.

Share this post


Link to post
Share on other sites

Perfect, just what i was looking for.

Share this post


Link to post
Share on other sites

Hello !

I searched a way to easily backup my au3 scripts from work to home and vice versa.

I found no (simple) equivalent to rsync on windows.

'DeltaCopy' is a simple rsync tool for windows. Did you try that ?


My Scripts[topic="73325"]_ReverseDNS()[/topic]Favourite scripts by other members[topic="81687"]SNMP udf[/topic][topic="70759"]Using SNMP - MIB protocol[/topic][topic="39050"]_SplitMon:Section off your monitor!, split your monitor into sections for easy management[/topic][topic="73425"]ZIP.au3 UDF in pure AutoIt[/topic][topic="10534"]WMI ScriptOMatic tool for AutoIt[/topic][topic="51103"]Resources UDF embed/use any data/files into/from AutoIt compiled EXE files[/topic]

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Thanks for this excellent script.

I have modified it to my own needs. I wanted to sync ppt files to a working directory, also the ones that do not exist in the source should be deleted. Also it should not copy any ppt file that is opened for edit.

Func Sync($from, $to, $ext, $withdelete)
; A modification from:
; http://www.autoitscript.com/forum/index.php?showtopic=29715

    ; Building the logical source directory
    Local $returnVal = False
    Local $FilesFrom = FileFindFirstFile($from & "*." & $ext)

    While 1
        Local $file = FileFindNextFile($FilesFrom)
        If @error Then ExitLoop
        ; Building the full path to source and destination file (or directory)
        Local $filename = FileGetLongName($from & $file)
        Local $tofilename = FileGetLongName($to & $file)
        
        ; Getting the mod. time of source and dest file...
        Local $fromtime = FileGetTime($filename, 0, 1)
        Local $totime = FileGetTime($tofilename, 0, 1)
        
        ; In case the destination file does not exists, $fromtime is always greater than $totime
        If $fromtime > $totime and not(FileLocked ($filename)) Then
            FileCopy($filename, $tofilename, 9)
            $returnVal = True
        EndIf
    WEnd
    ; Proper close of the file list
    FileClose($FilesFrom)
    
    ; If True, we should delete the files in the destination dir that do not exist in the source anymore
    If $withdelete Then
        Local $FilesTo = FileFindFirstFile($from & "*." & $ext)
        While 1
            $file = FileFindFirstFile($FilesTo)
            If @error Then ExitLoop
            ; Building the full path to source and destination file (or directory)
            Local $filename = FileGetLongName($from & $file)
            Local $tofilename = FileGetLongName($to & $file)
            
            ;if the file in the from-dir does not exist, we should delete it in the dest-dir
            if not(FileExists ($fileName)) then 
                FileDelete($tofileName)
                $returnVal = True
            EndIf
        WEnd
        FileClose($FilesTo)
    EndIf
    ; Return whether some copy/delete action took place
    return $returnVal
EndFunc

Func FileLocked ($fileName)
    ; Checks if a file is locked
    local $hFile = fileopen($fileName,1)
    Return $hFile <> -1 
    fileclose ($hFile)
EndFunc
Edited by Bart74

Share this post


Link to post
Share on other sites

Works great!

Is it possible to add a progressbar to the script?!

Thanx

Share this post


Link to post
Share on other sites

Is it possible to add a progressbar to the script?!

Everything is possible.

BUT the _RSync func is recursive so it's a bit crappy.

At the begenning, add :

Global $FileCount

Global $FileProg

Before the 2 calls to func _RSync() add :

Local $NbFilesFrom = DirGetSize($PathFrom, 1)

Local $NbFilesTo = DirGetSize($PathTo, 1)

If $NbFilesFrom[1] >= $NbFilesTo[1] Then ; Number of processed files = max($NbFilesFrom, $NbFilesTo) * 2, for 2 rsyncs are done

$FileCount = $NbFilesFrom[1] * 2

Else

$FileCount = $NbFilesTo[1] * 2

EndIf

$FileProg = $FileCount

ProgressOn("Rsync", "Synchronizing...") ; Let's go for a progress bar

And just after, ProgressOff() of course.

In the _RSync func, just before the WEnd, add :

ProgressSet($FileProg*100/$FileCount, "Remaining : " & $FileProg)

$FileProg -= 1

And you're done :)

Apzo.

Share this post


Link to post
Share on other sites

Everything is possible.

BUT the _RSync func is recursive so it's a bit crappy.

At the begenning, add :

Global $FileCount

Global $FileProg

Before the 2 calls to func _RSync() add :

Local $NbFilesFrom = DirGetSize($PathFrom, 1)

Local $NbFilesTo = DirGetSize($PathTo, 1)

If $NbFilesFrom[1] >= $NbFilesTo[1] Then ; Number of processed files = max($NbFilesFrom, $NbFilesTo) * 2, for 2 rsyncs are done

$FileCount = $NbFilesFrom[1] * 2

Else

$FileCount = $NbFilesTo[1] * 2

EndIf

$FileProg = $FileCount

ProgressOn("Rsync", "Synchronizing...") ; Let's go for a progress bar

And just after, ProgressOff() of course.

In the _RSync func, just before the WEnd, add :

ProgressSet($FileProg*100/$FileCount, "Remaining : " & $FileProg)

$FileProg -= 1

And you're done :)

Apzo.

Thanx, I'll try that.

Share this post


Link to post
Share on other sites

Everything is possible.

BUT the _RSync func is recursive so it's a bit crappy.

At the begenning, add :

Global $FileCount

Global $FileProg

Before the 2 calls to func _RSync() add :

Local $NbFilesFrom = DirGetSize($PathFrom, 1)

Local $NbFilesTo = DirGetSize($PathTo, 1)

If $NbFilesFrom[1] >= $NbFilesTo[1] Then ; Number of processed files = max($NbFilesFrom, $NbFilesTo) * 2, for 2 rsyncs are done

$FileCount = $NbFilesFrom[1] * 2

Else

$FileCount = $NbFilesTo[1] * 2

EndIf

$FileProg = $FileCount

ProgressOn("Rsync", "Synchronizing...") ; Let's go for a progress bar

And just after, ProgressOff() of course.

In the _RSync func, just before the WEnd, add :

ProgressSet($FileProg*100/$FileCount, "Remaining : " & $FileProg)

$FileProg -= 1

And you're done :)

Apzo.

This works but the status bar goes backwards. ;) It shows a 100% done, then goes down to zero.

Share this post


Link to post
Share on other sites

I was wondering if there is a way to make this synchonize a local directory with a folder on a server via ftp. But i can't think of any ways to insert the FTP folder correctly. Any suggestions?

Share this post


Link to post
Share on other sites

If you want to use a commmand line utility - check out Robocopy - it came with a Microsoft Resource Kit awhile back but can be found online. It offers a 'mirror' file copy option.

Share this post


Link to post
Share on other sites

If you want to use a commmand line utility - check out Robocopy - it came with a Microsoft Resource Kit awhile back but can be found online. It offers a 'mirror' file copy option.

If your interested in Robocopy you coudl check out my Robocopy function. it makes life a lot easier.

http://www.autoitscript.com/forum/index.php?app=forums&module=forums&section=findpost&pid=843922

Good luck

John Morrison

Share this post


Link to post
Share on other sites

Another way to sync dirs is to use a GNU port for windows of the well-known unix command 'cp'.

Check the -u option :

cp --help

...

-u, --update

copy only when the SOURCE file is newer than the destination file or when the destination file is missing

I use it twice : source -> destination then destination -> source.

Apzo.

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