Jump to content

Synchronize the content of 2 directories


Apzo
 Share

Recommended Posts

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
Link to comment
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.

Link to comment
Share on other sites

  • 6 months later...
  • 1 year later...
  • 4 weeks later...
  • 1 month later...

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

  • 8 months later...

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
Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

  • 1 year later...

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

  • 3 months later...

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

Link to comment
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.

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...