WatchFolder() - updated on 2021-10-14

Post your working scripts, libraries and tools for AHK v1.1 and older
wer
Posts: 57
Joined: 29 Nov 2022, 21:28

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

Post by wer » 28 Jan 2023, 04:49

dear just me, thanks for this great work
if i use the code below,
when the new file did downloaded (which is not tmp or crdownload),
it will show the msgbox, and the watch action will stop automatically, right?
and my question is, how to set a timeout, when the watch action got no result (no new file added) after a period
redo some action and watch again, and again, until redo times reach to a seted limit
thanks sir

Code: Select all

WatchFolder(download_folder, "ReportFunction")
ReportFunction(Directory, Changes) {
   Static MyExtensions := "tmp,crdownload"
   For Each, Change In Changes {
      Action := Change.Action
      Name := Change.Name
      SplitPath, Name, , , Extension
      If Extension In MyExtensions ; skip files depending on the extension
         Continue
      ; -------------------------------------------------------------------------------------------------------------------------
      ; Action 1 (added) = File gets added in the watched folder
      If (Action = 1)
         MsgBox, %Name%
      }
}

just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

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

Post by just me » 28 Jan 2023, 05:20

@wer,

I'm not sure about your purpose.
You can call

Code: Select all

Watchfolder("**PAUSE", True/False)
to pause/resume watching or

Code: Select all

WatchFolder("**END")
to stop it at any time.

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

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

Post by wer » 28 Jan 2023, 05:47

@just me
right sir, my purpose is like this:
i have a bunch of urls need to download,
so i want to keep watching the download folder
for when new file added, record its name and modified time
consider the download may failed sometime,
so just in case, if there is no new file added after a while
gonna need retry the download action a several times instead of just watching
i use a endless loop for these urls, so i prefer do watchfolder() once in every repeat
instead of keep the watchfolder() activated all the time
(seems watchfolder() just rush forward, never waiting for file download finished,
so the new added file can not be recorded appropriately)
could you teach me how to achieve this? thanks sir
Last edited by wer on 28 Jan 2023, 10:08, edited 1 time in total.

MrDoge
Posts: 159
Joined: 27 Apr 2020, 21:29

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

Post by MrDoge » 28 Jan 2023, 10:03

@wer
why can't you use a download manager ? though I don't know any download managers
download manager can retry downloads if failed
download manager has the list of files downloaded

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

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

Post by wer » 28 Jan 2023, 10:16

MrDoge wrote:
28 Jan 2023, 10:03
@wer
why can't you use a download manager ? though I don't know any download managers
download manager can retry downloads if failed
download manager has the list of files downloaded
right sir, thanks for the reply
thats because i need to record each downloaded file's name and modified time
and correspond the original download url
i use loop file method before, but with the amount of files increased,
it will skip the file which contains "[]" in name, strange question,
so i try to look for other solution and come to this topic

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

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

Post by wer » 28 Jan 2023, 22:47

@just me
k got it preliminarily:

Code: Select all

open_url(url){
	new_file_name:=""
	new_file_time:=""
	new_file_created:=0
	Run, % "msedge.exe " url
	WatchFolder(download_folder, "ReportFunction")
	loop, {
	   sleep, 1000
	   if new_file_created=1
	   break
	}
	WatchFolder("**END", True)
}

ReportFunction(Directory, Changes) {
   For Each, Change In Changes {
      Action := Change.Action
      Name := Change.Name
      SplitPath, Name, , , Extension
      ; Action 1 (added) = File gets added in the watched folder
      If (Extension="tmp")
         continue
      If (Extension="crdownload")
         continue
      SplitPath, Name, new_file_name
      FileGetTime, new_file_time, %Name%, M
      new_file_created=1
   }
}
this seems could waiting for download finished then report it appropriately
thanks for your help sir

with retry mechanism, may like this:

Code: Select all

open_url(url){
	new_file_name:=""
	new_file_time:=""
	new_file_created:=0
	loop_times:=0
	Run, % "msedge.exe " url
	WatchFolder(download_folder, "ReportFunction")
	loop, {
	   sleep, %repeat_ms%
	   if new_file_created=1
	   break
	   if (loop_times=loop_limit){
	      loop_times:=0
	      break
	   }
	   loop_times++
	}
	WatchFolder("**END", True)
	if (new_file_created=0 and retry_time=retry_limit) {
    new_file_name:=""
    new_file_time:=""
  } else if (new_file_created=0 and retry_time<retry_limit) {
    retry_time++
    open_url(url)
  }
}

ReportFunction(Directory, Changes) {
   For Each, Change In Changes {
      Action := Change.Action
      Name := Change.Name
      SplitPath, Name, , , Extension
      ; Action 1 (added) = File gets added in the watched folder
      If (Extension="tmp")
         continue
      If (Extension="crdownload")
         continue
      SplitPath, Name, new_file_name
      FileGetTime, new_file_time, %Name%, M
      new_file_created=1
   }
}

bobmac1547
Posts: 5
Joined: 05 Dec 2015, 21:05

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

Post by bobmac1547 » 18 Feb 2023, 19:16

Hey justme...

I found your script but, not being a "real programmer" I'm lost as to how to modify it. Here's my problem...

MS Edge insists (and there's no other way according to MS :twisted: ) on opening PDF files in its own internal viewer unless you opt to download it. But then there's the extra step to go to the download folder and manually open it with the preferred/default PDF application.

I want script in AHK to help me. Basically, it would detect a downloaded PDF file in the Downloads directory and then open it in the default PDF application, which, in my case, is NitroPDF.

Can you help me with this?
I measure a good day by how little I fell behind
Once you've seen one Parkinson's patient, you've seen one Parkinson's patient. http://parkinson.org

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

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

Post by boiler » 18 Feb 2023, 23:14

bobmac1547 wrote: I found your script but, not being a "real programmer" I'm lost as to how to modify it.

Basically, it would detect a downloaded PDF file in the Downloads directory and then open it in the default PDF application, which, in my case, is NitroPDF.
You wouldn’t modify it. You would use the function as written and have it watch for when files are added to the Downloads directory and call your custom function that would simply Run the file path of the identified newly added file, which should cause it to open using your default PDF viewer. See the comments at the beginning the WatchFolder() function for how to set up the parameters you would pass in your function call to do that. Make an attempt, and ask questions on specifics if necessary.

Jazzster56
Posts: 1
Joined: 26 May 2023, 09:21

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

Post by Jazzster56 » 26 May 2023, 09:30

Brand new in this forum. Just joined today.
Just created these two scripts by copying and pasting into new ahk files. The sample script GUI comes up fine. I set it to a folder and hit start. I add a new file to the watched folder and nothing shows on the GUI. It seems to be working for just about everyone else. What could be wrong?

just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

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

Post by just me » 28 May 2023, 06:38

Did you press the Start button after you selected the new folder?

punkzappa09
Posts: 1
Joined: 07 Dec 2022, 03:40

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

Post by punkzappa09 » 07 Mar 2024, 01:03

hi need some help..

Code: Select all

sourceFile=D:\steam\_FONGCHA_\video_settings"

WatchFolder("D:\steam\userdata", "ReportFunction",, 1) ; change the path of course

ReportFunction(Directory, Changes)
	{
	 For Each, Change In Changes
		{
		 Action := Change.Action
		 Name := Change.Name
RunWait cmd.exe /c xcopy /E /Y "%sourceFile%\570\local\cfg\video.txt" "%Name%",,Hide
		}
	}
[Mod edit: Added [code][/code] tags. Please use them yourself when posting code!]

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

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

Post by LAPIII » 05 Apr 2024, 11:43

You should convert this a very useful script for v2.

gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

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

Post by gregster » 05 Apr 2024, 11:48

LAPIII wrote:
05 Apr 2024, 11:43
You should convert this a very useful script for v2.
See viewtopic.php?t=95659#p425243.

Post Reply

Return to “Scripts and Functions (v1)”