Monitoring a directory for new files being added
Monitoring a directory for new files being added
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?
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?
- NoahPhense
- Addict
- Posts: 1999
- Joined: Thu Oct 16, 2003 8:30 pm
- Location: North Florida
Re: Monitoring a directory for new files being added
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
If anyone has any previous code, or has help with this API please jump
in... (cough cough Sparkie)
- np
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
PB 5.21 LTS (x86) - Windows 8.1
- NoahPhense
- Addict
- Posts: 1999
- Joined: Thu Oct 16, 2003 8:30 pm
- Location: North Florida
-
- User
- Posts: 84
- Joined: Sat Jul 19, 2003 6:45 pm
- Location: UK
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
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
-
- Enthusiast
- Posts: 767
- Joined: Sat Jan 24, 2004 6:56 pm
dell_jockey,
You can use multiple notification options, but you have to OR them together, not AND them, like this:
Regards,
Eric
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
Eric
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.
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
PB 5.21 LTS (x86) - Windows 8.1