WatchFolder() - updated on 2021-10-14

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: WatchFolder() - updated on 2016-11-30

27 Jan 2021, 09:17

Usually #Persistent is not required because there's some other AHK functionality that is used in the script which automatically makes it persistent. However, in that most basic example, it is required.
#Persistent wrote:This is usually unnecessary because the script is automatically persistent if the program detects that it has some way of launching new threads, such as a hotkey, timer or GUI.
This example has none of these, hence, #Persistent.
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: WatchFolder() - updated on 2016-11-30

27 Jan 2021, 09:41

@kczx3
Thanks kcx3! At last everything fell into place and the script works great as expected. I had realized #Persistent was missing (before I checked your mention) so I added that and tested but still no good. I then added False (after I checked your reply) and that did the trick! Thanks again, and as a final note, I didn't have to change the Default: 0x03 or any other parameters in the original function.
Skrell
Posts: 302
Joined: 23 Jan 2014, 12:05

Re: WatchFolder() - updated on 2016-11-30

14 Feb 2021, 10:46

Hi there, I've just started using this fantastic script to monitor 5 directories for new files downloaded/created and then kick off an exe which scans them for viruses. I run the script as an admin, but at some point after running it hangs forever... Do you have any suggestion for figuring out why? My script is quite minor so I feel like it can't be my code as it works fine for a bit (hence I must have registered my function correctly with Watchfolder()..)

My other thought is maybe it has something to do with setting subtree option to True?
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: WatchFolder() - updated on 2016-11-30

14 Feb 2021, 11:52

@Skrell - Even if your script doesn’t have a lot of lines, it can still be the source of the problem. For example, using RunWait could have your script waiting on your virus scan software to close, causing your script to hang up. You should post your script so the forum can determine if there are any potential problems in it.
Skrell
Posts: 302
Joined: 23 Jan 2014, 12:05

Re: WatchFolder() - updated on 2016-11-30

14 Feb 2021, 12:06

boiler wrote:
14 Feb 2021, 11:52
@Skrell - Even if your script doesn’t have a lot of lines, it can still be the source of the problem. For example, using RunWait could have your script waiting on your virus scan software to close, causing your script to hang up. You should post your script so the forum can determine if there are any potential problems in it.
I might have fixed it, and your intuition was right! I think it was my fault!! doh!
viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: WatchFolder() - updated on 2016-11-30

19 Feb 2021, 12:36

how to exit watch?

I use a global var to break.
I don't know if it's over yet

Code: Select all

WatchFolder("F:\Documents\Pictures\", "ReportFunction",false,1)

ReportFunction(Directory, Changes) {
global end
 For Each, Change In Changes {
       Action := Change.Action
       Name := Change.Name
    if  (end)
        Break
    If (Action = 1)
        msgbox %name%
       }
}

#k:: 
end := 1
return
and the SubTree := False dont work?
I watch "F:\Documents\Pictures\"
but when i move file to "F:\Documents\Pictures\test"
it still msgbox %name%
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: WatchFolder() - updated on 2016-11-30

22 Feb 2021, 02:46

Code: Select all

; Parameters:
;     Folder      -  The full qualified path of the folder to be watched.
;                    Pass the string "**PAUSE" and set UserFunc to either True or False to pause respectively resume watching.
;                    Pass the string "**END" and an arbitrary value in UserFunc to completely stop watching anytime.
;                    If not, it will be done internally on exit.
;     UserFunc    -  The name of a user-defined function to call on changes. The function must accept at least two parameters:
;                    1: The path of the affected folder. The final backslash is not included even if it is a drive's root
;                       directory (e.g. C:).
;                    2: An array of change notifications containing the following keys:
;                       Action:  One of the integer values specified as FILE_ACTION_... (see below).
;                                In case of renaming Action is set to FILE_ACTION_RENAMED (4).
;                       Name:    The full path of the changed file or folder.
;                       OldName: The previous path in case of renaming, otherwise not used.
;                       IsDir:   True if Name is a directory; otherwise False. In case of Action 2 (removed) IsDir is always False.
;                    Pass the string "**DEL" to remove the directory from the list of watched folders.
User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: WatchFolder() - updated on 2016-11-30

10 Oct 2021, 03:40

Hello @just me , Thanks you too much for that a useful code ,
How i can extract that table to MS.excel or notepad please ?
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: WatchFolder() - updated on 2016-11-30

10 Oct 2021, 04:59

Which 'table' do you mean?
User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: WatchFolder() - updated on 2016-11-30

10 Oct 2021, 05:28

I mean that result which it appear in ListView
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: WatchFolder() - updated on 2016-11-30

11 Oct 2021, 04:10

The ListView is not part of the class. The changes are passed to the UserFunc as an array. You can check the MyUserFunc in the sample script to see how this array can be processed.
User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: WatchFolder() - updated on 2016-11-30

11 Oct 2021, 05:35

I get it , Thanks for the clarification
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: WatchFolder() - updated on 2021-10-14

15 Oct 2021, 02:46

*bug-fix 1.0.03.00*
MrDoge
Posts: 160
Joined: 27 Apr 2020, 21:29

Re: WatchFolder() - updated on 2021-10-14

15 Oct 2021, 08:30

https://github.com/AHK-just-me/WatchFolder/commit/55f280a472ab3a793a2aca37def9f561b2b04427?diff=split
omg, thank you for fixing this
but I can't understand how you did it
___
aha, recently I have learned how to use a debugger, https://github.com/zero-plusplus/vscode-autohotkey-debug
is it when you RemoveAt, the array reorders ?
so I've been able to use 1 (order doesn't matter), but not 2 (order matters)

I think I've found a bug

Code: Select all

      VarSetCapacity(LongPath, MAX_DIR_PATH << !!A_IsUnicode, 0)
      If !DllCall("GetLongPathName", "Str", Folder, "Ptr", &LongPath, "UInt", MAX_DIR_PATH)
[in] cchBuffer

The size of the buffer lpszLongPath points to, in TCHARs.
shouldn't it be the same ?
MAX_DIR_PATH << !!A_IsUnicode and MAX_DIR_PATH

to get long path, I propose to use:

Code: Select all

Loop, Files, % Folder, D
{
  Folder:=A_LoopFileLongPath
}
___

Code: Select all

      For Event, Folder In EventArray {
         DllCall("CloseHandle", "Ptr", Folder.Handle)
         DllCall("CloseHandle", "Ptr", Event)
      }
this code is very cool
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: WatchFolder() - updated on 2021-10-14

15 Oct 2021, 09:07

@MrDoge,
... is it when you RemoveAt, the array reorders ?
Yes! ;)
I think I've found a bug ...
I don't think so. VarSetCapacity requires to specify RequestedCapacity as bytes, not as TCHARs.
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: WatchFolder() - updated on 2021-10-14

15 Oct 2021, 10:21

How VATCHFOLDER V2 Works with Beta
MrDoge
Posts: 160
Joined: 27 Apr 2020, 21:29

Re: WatchFolder() - updated on 2021-10-14

15 Oct 2021, 13:44

@just me
TCHARs
oof, documentation, I should've questionned more why you changed it
___

now I am able to rewrite it, excluding the DllCall parts (where do you even find which WinAPI function? (CreateFile, GetOverlappedResult) without a C++ background it is hard to understand https://docs.microsoft.com/en-us/windows/win32/fileio/obtaining-directory-change-notifications)
___

I wanted to separate the SetTimer and etc. so there's no need for If (Folder = TimerID) { ; called by timer
this.I this.don't this.really this.like this.to this.use this.classes, but I think I have to: to only use 1 global variable name : WatchFolder

there isn't any difference in functionality (I hope):

Code: Select all

WatchFolder.Add(Folder, UserFunc, SubTree := False, Watch := 0x03)
WatchFolder.Remove(Folder)
WatchFolder.RemoveAll()
WatchFolder.Pause()
WatchFolder.UnPause() ;Resume is too easily mistaken as Remove
3 files here: https://github.com/FuPeiJiang/WatchFolder/tree/class/class
  • performance: do not use .Count() for routine: expensive
    cons: manually counting this.EventToFolderinfo_Count, prone to errors?
  • some UI changes in WatchFolder2_sample.ahk
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: WatchFolder() - updated on 2021-10-14

16 Oct 2021, 04:56

@MrDoge:
... to only use 1 global variable name : WatchFolder
How many global names do you need when using the function?


... performance: do not use .Count() for routine: expensive
Why do you think that? The method call just returns the contents of a built-in counter:

Code: Select all

ResultType Object::_Count(ExprTokenType &aResultToken, ExprTokenType *aParam[], int aParamCount)
{
	aResultToken.SetValue((__int64)mFieldCount);
	return OK;
}


Code: Select all

         ; DllCall( "GlobalFree", "Ptr",this.WaitObjectsPtr ) ;turns out, this is already freed by _Remove()? so it crashes when I try to free it?
You might be wrong.
GHND 0x0042 -> Combines GMEM_MOVEABLE and GMEM_ZEROINIT.
The movable-memory flags GHND and GMEM_MOVABLE add unnecessary overhead and require locking to be used safely. They should be avoided unless documentation specifically states that they should be used.
Source: GlobalAlloc()
icc2icc
Posts: 18
Joined: 04 Aug 2021, 21:24

Re: WatchFolder() - updated on 2021-10-14

31 Jan 2022, 19:05

Hi @just me
I really appreciate for your great work
just 1 question
is your code cleaning up memory when exiting ? or should I add an on exit function?
Spoiler
icc2icc
Posts: 18
Joined: 04 Aug 2021, 21:24

Re: WatchFolder() - updated on 2021-10-14

31 Jan 2022, 19:42

seems that your DummyObject does that , would you explain me how?

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 149 guests