WatchFolder() - updated on 2016-11-30

Post your working scripts, libraries and tools
just me
Posts: 5457
Joined: 02 Oct 2013, 08:51
Location: Germany

WatchFolder() - updated on 2016-11-30

21 Jun 2015, 05:27

This is a rewrite of HotKeyIt's WatchDirectory(). It began when I tried to understand how WatchDirectory() is working and therefore 'translated' HotKeyIt's into my programming style. After a short discussion we decided that my version should be published discretely using a different name, so WatchFolder() was born.

Advantages (subjective):
  • no dependencies.
  • easy to use parameters passed to the user function.
Disadvantages (objective):
  • No filter options.
Credits:
  • HotKeyIt for WatchDirectory().
  • zcooler for extensive testing.
Change History:
    Update on 2016-11-30 - v1.0.02.00
    • bug-fix for closing handles
    Update on 2016-03-14 - v1.0.01.00
    • bug-fix for multiple folders
    Initial Release on 2015-06-21 - v1.0.00.00
WatchFolder.ahk:

Code: [Select all] [Expand] [Download] (WatchFolder.ahk)GeSHi © Codebox Plus


WatchFolder_sample.ahk:

Code: [Select all] [Expand] [Download] (WatchFolder_sample.ahk)GeSHi © Codebox Plus



WatchFolder() at GitHub
Last edited by just me on 14 Mar 2016, 05:42, edited 4 times in total.
User avatar
TheDewd
Posts: 1031
Joined: 19 Dec 2013, 11:16
Location: USA

Re: WatchFolder()

23 Jun 2015, 10:16

It's interesting to watch the root of the C:\ drive with all the options enabled.
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
User avatar
Joe Glines
Posts: 550
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: WatchFolder()

23 Jun 2015, 20:39

I love the demo! I pointed it to my C drive and included sub directories. Then I launched one of my webscraping scripts and watched the files being added/deleted from my hard drive go crazy! Very useful function and demo!
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: WatchFolder()

04 Jul 2015, 05:47

Wow, I wonder why I was so late to discover this must-have! :morebeard:
pom

Re: WatchFolder()

14 Aug 2015, 09:07

Good job just me!
How to watch multiple folders?
This? WatchFolder("C:\Videos", "MyUserFunc"), WatchFolder("C:\Music", "MyUserFunc")
This? WatchFolder(["C:\Videos", "C:\Music"], "MyUserFunc")
Or this? WatchFolder("C:\Videos|C:\Music", "MyUserFunc")
just me
Posts: 5457
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: WatchFolder()

14 Aug 2015, 09:53

; Folder - The full qualified path of the folder to be watched.

WatchFolder("C:\Videos", "MyUserFunc"), WatchFolder("C:\Music", "MyUserFunc")
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: WatchFolder()

23 Aug 2015, 06:31

By multiple, does it mean this can be extended to more than 2?: WatchFolder("C:\Videos", "MyUserFunc"), WatchFolder("C:\Music", "MyUserFunc") :morebeard:
just me
Posts: 5457
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: WatchFolder()

24 Aug 2015, 02:39

Yes.
WombatVengeance
Posts: 12
Joined: 29 Sep 2015, 11:36

Re: WatchFolder()

01 Oct 2015, 12:41

I've tried to figure this out for the last couple of hours, but without any simple examples, I can't make any sense of this. I just want to open a file when it is created in a specific directory. Can somebody please give an example of what that would look like?
zcooler
Posts: 455
Joined: 11 Jan 2014, 04:59

Re: WatchFolder()

01 Oct 2015, 13:37

WombatVengeance wrote:I've tried to figure this out for the last couple of hours, but without any simple examples, I can't make any sense of this. I just want to open a file when it is created in a specific directory. Can somebody please give an example of what that would look like?

Perhaps like this:
Spoiler
WombatVengeance
Posts: 12
Joined: 29 Sep 2015, 11:36

Re: WatchFolder()

01 Oct 2015, 13:49

Thank you!
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: WatchFolder()

04 Oct 2015, 07:42

zcooler, your example was very helpful!
roflcoopter

Re: WatchFolder()

07 Oct 2015, 04:55

Hello! I am getting an error in the WatchFolder function. It says Target label doesnt exist and points to the first line with SetTimer, % Timerfunc, Off.

What am i doing wrong?
User avatar
Nextron
Posts: 1207
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win7 x64 AHK: Unicode x32

Re: WatchFolder()

07 Oct 2015, 05:05

You're using an old version of AutoHotkey. Download the most recent.
roflcoopter

Re: WatchFolder()

07 Oct 2015, 06:18

Oh okay, too bad. Im on my workcomputer and we cant just install or download new software. Is there any other workaround or similar function for older versions of AHK?
User avatar
Nextron
Posts: 1207
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win7 x64 AHK: Unicode x32

Re: WatchFolder()

07 Oct 2015, 07:11

Which version are you using? Because this error will not be the only incompatibility. Using function references in timers was added in 1.1.20.00, which also added Func.Bind(args*), which is used in this function.

You could try something like this (untested)
Or use WatchDirectory() from 2010 which is mentioned in the top post.
roflcoopter

Re: WatchFolder()

08 Oct 2015, 01:23

Tried the code, im not getting the error now atleast but still cant get it to work. I'm on version 1.1.13.00. Ill check out WatchDirectory.

Appreciate the help!
-_+
Posts: 70
Joined: 06 Dec 2014, 12:43

Re: WatchFolder()

09 Feb 2016, 12:38

just me, could you please make the script distinguish file move (as a single event)?
Currently, when a file is being moved - your script shows it as 2 events: old file was deleted, new one was created.
just me
Posts: 5457
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: WatchFolder()

09 Feb 2016, 16:35

... your script shows it as 2 events: old file was deleted, new one was created.
That's exactly what happens in the file system in case of 'move'.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: feiyue, ptpt and 27 guests