Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[AHK_L/v2] WatchDirectory - Report Directory Changes


  • Please log in to reply
158 replies to this topic
cjsmile999
  • Members
  • 78 posts
  • Last active: Nov 18 2016 05:24 AM
  • Joined: 29 Jun 2009

Hello Hoping you can help me out.  I'm monitor a folder and when a file is created/dropped in the director I need to move it.  I have two problems:

 

1.  It seems to report when a new file is dropped into the directory (great :)), however it reports when it's moved as well.  Even though I'm specifcally looking for creation only.

 

2.  It doesn't seem to respect my specific file type.  It moves any file that is dropped in there.

 

Below is the code I'm using.

WatchFolders = C:\Drop|.cdx\

Loop,Parse,WatchFolders,|
WatchDirectory(A_LoopField,"ReportChanges",0x1)
,LV_Add("",SubStr(A_LoopField,0)="*" ? (SubStr(A_LoopField,1,StrLen(A_LoopField)-1)) : A_LoopField
         ,SubStr(A_LoopField,0)="*" ? 1 : 0)
LV_ModifyCol(1,"AutoHdr")

#Persistent
OnExit, Close
Return

ReportChanges(from, to)
{
msgbox, detected drop
FileMove, C:\Drop\trans_11150193.cdx, C:\Processing\ 
}


Close:
WatchDirectory()
ExitApp


HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

Try that:

WatchFolders = x:\|.cdx\

WatchDirectory(WatchFolders,"ReportChanges",0x1)

#Persistent
OnExit, Close
Return

ReportChanges(from, to)
{
	if !to ; file deleted or moved
		return
msgbox, detected drop
SplitPath,from,file
FileMove, %from%, C:\Processing\%file%
}


Close:
WatchDirectory()
ExitApp


cjsmile999
  • Members
  • 78 posts
  • Last active: Nov 18 2016 05:24 AM
  • Joined: 29 Jun 2009

Worked beautifully...thanks!!



bobycom
  • Members
  • 40 posts
  • Last active: Apr 14 2019 07:50 AM
  • Joined: 25 Mar 2014

HotKeyIt, what you wrote is in conflict with the "How to use" guide. Or I may be wrong.
 
"All you need is a function in your script that accepts 2 parameters
- When a file is changed, both parameters will contain same filename/path
- When a file is deleted, it is passed in first parameter and second parameter will be empty
- When a new file is created, first parameter is empty and second contains filename/path
- When a file is renamed, first parameter contains old filename/path and second new filename/path"
 
So IMO the code of the function should be:

ReportChanges(from, to)
{
	if !from
	{
		msgbox, detected drop
		SplitPath,to,file
		FileMove, %to%, C:\Processing\%file%
	}
}


HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

That does not matter, since we said notify only when a file is created/deleted.

It could be also:

ReportChanges(from, to)
{
	if to
	{
		msgbox, detected drop
		SplitPath,to,file
		FileMove, %to%, C:\Processing\%file%
	}
}


bobycom
  • Members
  • 40 posts
  • Last active: Apr 14 2019 07:50 AM
  • Joined: 25 Mar 2014

I agree. My point was that you used the file from the first parameter (from), which I think is empty at file creation event, instead of the second (to) for the extract file name and file move operations.



HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

You are right, I missed that  :)



bichlepa
  • Members
  • 62 posts
  • Last active: Feb 19 2019 08:39 PM
  • Joined: 04 Jul 2011

Thanks HotKeyIt,
 
I have included WatchDirectory into AutoHotFlow. There is now a trigger "File observer"



HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

Nice :D