Dereck21
asked on
Monitoring Directory for file Changes
I want a simle visual basic 6 program that looks at a directory (c:\printjobs) and when it finds a file it executes some code. I have a start on this but need some help.
Here is what I have so far:
Private Declare Function FindFirstChangeNotificatio n Lib "kernel32" Alias "FindFirstChangeNotificati onA" (ByVal lpPathName As String, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Const FILE_NOTIFY_CHANGE_FILE_NA ME = &H1
Private Const FILE_NOTIFY_CHANGE_ATTRIBU TES = &H4
Private Const FILE_NOTIFY_CHANGE_LAST_WR ITE = &H10
Private Sub Form_Load()
m_WaitHandle = FindFirstChangeNotificatio n("C:\prin tjobs", False, FILE_NOTIFY_CHANGE_LAST_WR ITE Or FILE_NOTIFY_CHANGE_FILE_NA ME Or FILE_NOTIFY_CHANGE_ATTRIBU TES)
End Sub
I know I need to use WaitForSingleObject(m_Wait Handle, 50) somewhere, but I cant figure out how to get it to work.
Thanks
Here is what I have so far:
Private Declare Function FindFirstChangeNotificatio
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Const FILE_NOTIFY_CHANGE_FILE_NA
Private Const FILE_NOTIFY_CHANGE_ATTRIBU
Private Const FILE_NOTIFY_CHANGE_LAST_WR
Private Sub Form_Load()
m_WaitHandle = FindFirstChangeNotificatio
End Sub
I know I need to use WaitForSingleObject(m_Wait
Thanks
Returns a list of all the files in the Scripts folder. If the computer has more than one scripts folder (for example, C:\Scripts and D:\Scripts), files will be returned from each of these folders. Just add what you want to happen when your conditions are met inside of the collection and each time your file pops up within the folder it will happen.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set colFiles = objWMIService. _
ExecQuery("Select * from CIM_DataFile where Path = '\\Scripts\\'")
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper
Set colFiles = objWMIService. _
ExecQuery("Select * from CIM_DataFile where Path = '\\Scripts\\'")
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.mentalis.org/apilist/FindFirstChangeNotification.shtml
Complete sample and good description:
http://vbnet.mvps.org/code/fileapi/watchedfolder.htm
Complete sample with some advanced extra functions:
http://www.mvps.org/vbvision/_samples/Watch_Directory_Demo.zip