The MASM Forum

General => The Campus => Topic started by: cman on September 30, 2014, 03:08:06 AM

Title: Detecting Change In Directory Contents
Post by: cman on September 30, 2014, 03:08:06 AM
What's the best method to have a program periodically check the contents of a directory ( to see if files in the directory have been added or deleted ). Is there a way to have a message sent to a program if the contents of a directory change? Thanks for any information.
Title: Re: Detecting Change In Directory Contents
Post by: dedndave on September 30, 2014, 03:28:56 AM
use GetFileAttributesEx (also used on folders) to fill a WIN32_FILE_ATTRIBUTE_DATA structure
it has creation time, last access time, and last write time, as FILETIME structures

you can use SetTimer/WM_TIMER to generate periodic checks
Title: Re: Detecting Change In Directory Contents
Post by: sinsi on September 30, 2014, 07:31:32 AM
ReadDirectoryChangesW (http://msdn.microsoft.com/en-us/library/windows/desktop/aa365465%28v=vs.85%29.aspx)
Title: Re: Detecting Change In Directory Contents
Post by: dedndave on September 30, 2014, 10:43:58 AM
didn't know about that one   :P
Title: Re: Detecting Change In Directory Contents
Post by: Gunther on October 01, 2014, 02:46:11 AM
Interesting function, Sinsi. Thank you.  :t

Gunther
Title: Re: Detecting Change In Directory Contents
Post by: morgot on October 01, 2014, 03:47:56 AM
FindFirstChangeNotification.
Title: Re: Detecting Change In Directory Contents
Post by: farrier on October 01, 2014, 03:50:52 AM
cman,

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365261(v=vs.85).aspx

Use:

   invoke   FindFirstChangeNotification, \
         addr dirpath, \
         FALSE, \
         FILE_NOTIFY_CHANGE_FILE_NAME


I used this when I had a problem printing on a Win xp Pro computer from a DOS program. I had the DOS program "print" to a file instead of "LPT1" and monitored the directory for the creation of the file.  Then used regular Win printing functions to print the file.  I submitted an example called "NotifyPrint" but can't seem to find it. 

hth,

farrier

morgot, also got it!
Title: Re: Detecting Change In Directory Contents
Post by: farrier on October 01, 2014, 09:29:53 AM
I found a short example (fasm syntax):

This would operate in a Thread, while your program does other stuff! Then is notified when a file change event occurs!

invoke FindFirstChangeNotification, \
addr pmpath, \
FALSE, \
FILE_NOTIFY_CHANGE_FILE_NAME

mov hPMNotify, eax

invoke WaitForSingleObject, hPMNotify, INFINITE

.while TRUE
DoYourFileChangeStuffHere:



invoke FindNextChangeNotification, hPMNotify
invoke WaitForSingleObject, hPMNotify, INFINITE
.endw


hth,

farrier