Is there a command for AHK to synchronize folders?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
daiweisc
Posts: 46
Joined: 19 Oct 2019, 04:27

Is there a command for AHK to synchronize folders?

23 Sep 2021, 02:58

I want to synchronize two folders, it is a one-way image copy.
What commands is used to execute in AHK?
User avatar
mikeyww
Posts: 26859
Joined: 09 Sep 2014, 18:38

Re: Is there a command for AHK to synchronize folders?

23 Sep 2021, 06:29

You could:

1. Loop, Files
2. See if the file exists in the other directory.
2.a. If not, copy it.
2.b. If so, FileGetSize; if newer, copy with overwrite.
3. If mirroring: loop through files in the target directory, recycling files that do not exist in the source.

A different approach for a mirror is to recycle the entire target directory, and then copy the source directory to the target directory. This ignores the possibility that the target directory might contain newer files.

Adjust the approach according to what you need to do with newer files, orphan files, etc.

Robocopy can handle just about every situation easily.
daiweisc
Posts: 46
Joined: 19 Oct 2019, 04:27

Re: Is there a command for AHK to synchronize folders?

23 Sep 2021, 08:13

Thanks.
If so, FileGetSize; if newer, copy with overwrite
Doesn’t it take time to judge the newness of the file?
User avatar
mikeyww
Posts: 26859
Joined: 09 Sep 2014, 18:38

Re: Is there a command for AHK to synchronize folders?

23 Sep 2021, 08:16

You are absolutely right, as every command that is executed does indeed require time. This applies not only to AHK but also Robocopy, commercial synchronizers, and all programs, for that matter. With a solid-state disk, many of these times are quite short and hardly noticeable with small numbers of files. You can time your script to judge whether the time is acceptable, or compare times with different approaches. A_TickCount can be used to mark the time. You can then subtract values as needed.

I meant to indicate using FileGetTime, rather than size, though some people prefer to use the size to gauge certain differences. For me, well, size is just not the highest priority....
User avatar
Chunjee
Posts: 1418
Joined: 18 Apr 2014, 19:05
Contact:

Re: Is there a command for AHK to synchronize folders?

23 Sep 2021, 13:05

I've had good luck with RoboCopy which comes with most versions of Windows out of the box. /mir is the option to "mirror" two directories
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy


You can drive it with ahk using RunWait or similar.

Code: Select all

Path_Source := "C:/Your folder path"
Path_Destination := "C:/Your folder path"
RunWait, %comspec% /c Robocopy "%Path_Source%" "%Path_Destination%" *.* /MIR /R:1,,hide
daiweisc
Posts: 46
Joined: 19 Oct 2019, 04:27

Re: Is there a command for AHK to synchronize folders?

23 Sep 2021, 16:09

Thanks a lot. It solve the problem.
daiweisc
Posts: 46
Joined: 19 Oct 2019, 04:27

Re: Is there a command for AHK to synchronize folders?

23 Sep 2021, 19:29

by the way ,

Code: Select all

RunWait, %comspec% /c Robocopy "%Path_Source%" "%Path_Destination%" *.* /MIR /R:1,,hide
what is the mean of "*.*"?
User avatar
Chunjee
Posts: 1418
Joined: 18 Apr 2014, 19:05
Contact:

Re: Is there a command for AHK to synchronize folders?

23 Sep 2021, 19:35

daiweisc wrote:
23 Sep 2021, 19:29
what is the mean of "*.*"?
Microsoft wrote: Specifies the file or files to be copied. Wildcard characters (* or ?) are supported. If you don't specify this parameter, *.* is used as the default value.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], haomingchen1998, mikeyww, mmflume, roysubs, scriptor2016, ShatterCoder and 101 guests