WatchFolder.ahk question Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
haomingchen1998
Posts: 175
Joined: 20 Feb 2023, 16:37

WatchFolder.ahk question

Post by haomingchen1998 » 30 Apr 2024, 17:22

I'm using WatchFolder.ahk from viewtopic.php?f=6&t=8384
I want to watch below actions for 2 predefined paths:

Code: Select all

;     FILE_ACTION_ADDED              = 1   (0x00000001) : The file was added to the directory.
;     FILE_ACTION_REMOVED            = 2   (0x00000002) : The file was removed from the directory.
;     FILE_ACTION_MODIFIED           = 3   (0x00000003) : The file was modified.
;     FILE_ACTION_RENAMED            = 4   (0x00000004) : The file was renamed (not defined by Microsoft).
Once any of the above action are detected, run AutoHotkeySyncTest function.

Code: Select all

AutoHotkeySyncPath1 := "C:\Users\Cody\Desktop\New folder\Ahk"
AutoHotkeySyncPath2 := "C:\Users\Cody\Desktop\New folder\Ahk2"

AutoHotkeySyncTest() {
   FolderSync(AutoHotkeySyncPath1, AutoHotkeySyncPath2)
}

FolderSync(Path1, Path2) {
  Run, %ComSpec% /c robocopy "%Path1%" "%Path2%" /MIR",, Hide
}

WatchFolder(AutoHotkeySyncPath1, "AutoHotkeySyncTest", True, 1|2|3|4)
WatchFolder(AutoHotkeySyncPath2, "AutoHotkeySyncTest", True, 1|2|3|4)
My code wasn't able to detect any of the actions, and I think the problem is this function "AutoHotkeySyncTest()" where I need to pass in some arguments? It should be something like this example below:

Code: Select all

MyUserFunc(Folder, Changes) {
   Static Actions := ["1 (added)", "2 (removed)", "3 (modified)", "4 (renamed)"]
   TickCount := A_TickCount
   GuiControl, -ReDraw, LV
   For Each, Change In Changes
      LV_Modify(LV_Add("", TickCount, Folder, Actions[Change.Action], Change.Name, Change.IsDir, Change.OldName, ""), "Vis")
   Loop, % LV_GetCount("Columns")
      LV_ModifyCol(A_Index, "AutoHdr")
   GuiControl, +Redraw, LV
}
But I'm don't understand this example code provided from WatchFolder_sample.ahk and how I can integrate that into my function.

Any advice would be greatly appreciated, thanks!

User avatar
boiler
Posts: 17146
Joined: 21 Dec 2014, 02:44

Re: WatchFolder.ahk question  Topic is solved

Post by boiler » 30 Apr 2024, 21:09

At least one issue is variable scope. Your AutoHotkeySyncTest() function is referencing global variables without them having been declared as global anywhere. Or like you said, you could pass them as parameters, but then you need to have that function define some parameters, then you need to bind them to your call from WatchFolder().

haomingchen1998
Posts: 175
Joined: 20 Feb 2023, 16:37

Re: WatchFolder.ahk question

Post by haomingchen1998 » 01 May 2024, 12:37

boiler wrote:
30 Apr 2024, 21:09
At least one issue is variable scope. Your AutoHotkeySyncTest() function is referencing global variables without them having been declared as global anywhere. Or like you said, you could pass them as parameters, but then you need to have that function define some parameters, then you need to bind them to your call from WatchFolder().
thanks!

Post Reply

Return to “Ask for Help (v1)”