Monitoring a directory for new files being added

Just starting out? Need help? Post your questions and find answers here.
Arbitrage
New User
New User
Posts: 4
Joined: Sun Sep 21, 2003 3:21 pm

Monitoring a directory for new files being added

Post by Arbitrage »

How would you monitor a directory for new files being added.
I know you could check the files against a list from time to time but
is there an easier way to get a windows message that a file is added
or changed?
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

Post by eriansa »

there's an api : "FindFirstChangeNotification" -> Google.
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Monitoring a directory for new files being added

Post by NoahPhense »

I need help with this too, I need to monitor some directories on the server.
If anyone has any previous code, or has help with this API please jump
in... (cough cough Sparkie) ;)

- np
batcherx
New User
New User
Posts: 9
Joined: Mon May 24, 2004 5:34 am

Post by batcherx »

Look up these four APIs:

FindFirstChangeNotificationA
MsgWaitForMultipleObjects
FindNextChangeNotification
FindCloseChangeNotification

to set up a loop to:

init, set monitoring time interval, get change item, terminate monitoring.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

I just threw this together and tested it on WinXPhome with PB 3.93. This will monitor for file creation and deletion of files in C:\. Any input is welcome for improvement. :)

Code: Select all

#FILE_NOTIFY_CHANGE_FILE_NAME = 1
; Any file name change in the watched directory or subtree
; causes a change notification wait operation to return.
; Changes include renaming, creating, or deleting a file name. 

#FILE_NOTIFY_CHANGE_DIR_NAME = 2
; Any directory-name change in the watched directory or
; subtree causes a change notification wait operation to return.
; Changes include creating or deleting a directory. 

#FILE_NOTIFY_CHANGE_ATTRIBUTES = 4
; Any attribute change in the watched directory or subtree causes
; a change notification wait operation to return. 

#FILE_NOTIFY_CHANGE_SIZE = 8
; Any file-size change in the watched directory or subtree causes
; a change notification wait operation to return. The operating
; system detects a change in file size only when the file is written
; to the disk. For operating systems that use extensive caching,
; detection occurs only when the cache is sufficiently flushed. 

#FILE_NOTIFY_CHANGE_LAST_WRITE = $10
; Any change to the last write-time of files in the watched directory
; or subtree causes a change notification wait operation to return. The
; operating system detects a change to the last write-time only when
; the file is written to the disk. For operating systems that use extensive
; caching, detection occurs only when the cache is sufficiently flushed. 

#FILE_NOTIFY_CHANGE_SECURITY = $100
; Any security-descriptor change in the watched directory or subtree
; causes a change notification wait operation to return. 

#INVALID_HANDLE_VALUE = - 1
#INFINITE = $FFFFFFFF
#STATUS_WAIT_0 = 0
#WAIT_OBJECT_0 = #STATUS_WAIT_0 + 0

Procedure FFCN()
  ; --> We'll check for file creation and deletion in c:\
  hFFCN = FindFirstChangeNotification_("c:\", #False, #FILE_NOTIFY_CHANGE_FILE_NAME)
  If hFFCN = #INVALID_HANDLE_VALUE
    MessageRequester("Error", "Invalid FindFirstChangeNotification.")
    ExitProcess_(0)
  EndIf
  While #True
    status = WaitForSingleObject_(hFFCN, #INFINITE)
    Select status
      Case #WAIT_OBJECT_0
        MessageRequester("Info", "Change occured in C:\")
        If FindNextChangeNotification_(hFFCN) = #False
          MessageRequester("Error", "Invalid FindNextChangeNotification")
          ExitProcess_(0)
        EndIf
    EndSelect
  Wend
EndProcedure
        
If OpenWindow(0, 0, 0, 300, 100, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "FindFirstChangeNotification" ) And CreateGadgetList(WindowID(0))
  ButtonGadget(0, 10, 10, 280, 20, "Create file: c:\Testing.txt")
  ButtonGadget(1, 10, 50, 280, 20, "Delete file: c:\Testing.txt")
  ; --> Our thread for change notifications
  CreateThread(@FFCN(), 0)
  Repeat
    event = WaitWindowEvent()
    Select event
      Case #PB_EventGadget
        Select EventGadgetID()
          Case (0)
            CreateFile(0, "c:\Testing.txt")
            CloseFile(0)
          Case 1
            DeleteFile("c:\Testing.txt")
        EndSelect
    EndSelect
  Until event = #PB_Event_CloseWindow
EndIf
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Damn sparkie.. you sure have the API stuff down!

To my knowledge, this is not in the codearchiv .. and should be added.

Thanks Sparkie, you saved me lots of time. Now I need to study the code
and learn what it's doing.

- np
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post by Beach »

Sparkie, do you know if the notification comes after the file has completed writing or just when the file appears in the directory? In a server scenario, it might take a few seconds for the file to complete transferring before being able to access it on the server...
-Beach
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

@NoahPhense: Thanks, but this one was easy. I just converted the example code from the SDK. ;)

@Beach: I'm not sure. I'll see if I can find any more info for us. :)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
spongehammer
User
User
Posts: 84
Joined: Sat Jul 19, 2003 6:45 pm
Location: UK

Post by spongehammer »

Beach: The notification occurs the second the file is created and not when
writing is complete.

Chris
I looked up 'paranoid' in the dictionary this morning.
It said, 'what do you want to know that for?'
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Sparkie!, Very useful thanks for sharing with us.
I have a server as well and this will keep me informed of any changes, now I must find the way to know which files has been changed hehe.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

@dagcrack: Your welcome. :)

When I get time, I'll take a look at ReadDirectoryChangesW_(). This should identify the file for you. It's only good for NT/2000/XP/2003. ;)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Arbitrage
New User
New User
Posts: 4
Joined: Sun Sep 21, 2003 3:21 pm

Post by Arbitrage »

Wow nice code. I needed it for some archiving stuff. Most copiers today
have the ability to scan to an ftp smb or proprietary server. Most of them
scan as TIFF, JPG and PDF. The mid range models scan at 70+ pages
per minute, which makes them great for archiving a large number of
documents quickly. I was going to play around with archiving some of
the documents at work that we have to keep for 5 years or more to save
a little space in the storage closet.

Thanks alot sparkie
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Sparkie, that's just great, thanks!

One question: can I AND the #FILE_NOTIFY_* options, to subscribe to multiple notifications at once?
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
ebs
Enthusiast
Enthusiast
Posts: 530
Joined: Fri Apr 25, 2003 11:08 pm

Post by ebs »

dell_jockey,

You can use multiple notification options, but you have to OR them together, not AND them, like this:

Code: Select all

#FILE_NOTIFY_CHANGE_FILE_NAME | #FILE_NOTIFY_CHANGE_ATTRIBUTES
Regards,
Eric
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

You can read more about FindFirstChangeNotification_() at msdn.

I also have the ReadDirectoryChangesW_() 90% working. I just need to tweak it a little bit and then I'll post the code. This will give you the name of the file and the change being made. :)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Post Reply