Link to home
Start Free TrialLog in
Avatar of Dereck21
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 FindFirstChangeNotification Lib "kernel32" Alias "FindFirstChangeNotificationA" (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_NAME = &H1
Private Const FILE_NOTIFY_CHANGE_ATTRIBUTES = &H4
Private Const FILE_NOTIFY_CHANGE_LAST_WRITE = &H10

Private Sub Form_Load()

m_WaitHandle = FindFirstChangeNotification("C:\printjobs", False, FILE_NOTIFY_CHANGE_LAST_WRITE Or FILE_NOTIFY_CHANGE_FILE_NAME Or FILE_NOTIFY_CHANGE_ATTRIBUTES)


End Sub

I know I need to use WaitForSingleObject(m_WaitHandle, 50) somewhere, but I cant figure out how to get it to work.

Thanks
Avatar of Ark
Ark
Flag of Russian Federation image

Brief description with small sample:
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


Avatar of TooKoolKris
TooKoolKris

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=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService. _
ExecQuery("Select * from CIM_DataFile where Path = '\\Scripts\\'")

For Each objFile in colFiles
     Wscript.Echo objFile.Name
Next


ASKER CERTIFIED SOLUTION
Avatar of waty
waty
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial