Need help creating a mirror image of a folder

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Chris70
Posts: 46
Joined: 17 Dec 2019, 12:25

Need help creating a mirror image of a folder

06 Mar 2020, 14:53

Hello AHKers -

I have a script that I can't seem to get to work correctly no matter how many iterations I try. It seems like this should be simple and I am missing something somewhere. Any help would be appreciated.

OK, so I want to look at a folder (Folder1). If the folder contains any files, I want to copy those files to another folder (Folder2). If Folder1 is empty, I want to clear Folder2 to match. I set this up to run every second. Essentially I am trying to create a mirror image of Folder 1. However, the script I have below seems to work on the first pass only (If Folder1 is empty, it clears Folder2 and if Folder1 has content, it copies it to Folder2), but Folder2 does not cycle back and forth (Empty, Full, Empty, Full, etc.) as the condition of Folder1 cycles between empty & full. Can someone please show me where I went wrong here or provide a better alternative?

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Persistent
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force

Gosub, CheckBatch
SetTimer, CheckBatch, 1000

CheckBatch:
{
Folder = [path of folder 1]
Folder1 = [path of folder 2]
Loop, Files, %Folder%\*.*, F
    TotalSize += A_LoopFileSize
If TotalSize <= 0
	FileDelete, %Folder1%\*.*
If TotalSize > 0
	FileCopy, %Folder%, %Folder1%
}
return
Thanks,
Chris70
User avatar
Chris70
Posts: 46
Joined: 17 Dec 2019, 12:25

Re: Need help creating a mirror image of a folder

06 Mar 2020, 15:06

OK, now I feel stupid. I think I figured it out. I was neglecting to set the initial folder size to zero on each iteration of the script. Once I changed it to the following, it worked! Thanks me for figuring that out!

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Persistent
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force

Gosub, CheckBatch
SetTimer, CheckBatch, 1000

CheckBatch:
{
Folder = [path of folder 1]
Folder1 = [path of folder 2]
TotalSize = 0
FileCopy, %Folder%, %Folder1%
Loop, Files, %Folder%\*.*, F
    TotalSize += A_LoopFileSize
If TotalSize <= 0
	FileDelete, %Folder1%\*.*
}
return

Return to “Ask For Help”

Who is online

Users browsing this forum: scriptor2016, songdg and 36 guests