icredes
asked on
trigger code on file copy
I want to be able to write code that will trigger when a file is copied into a folder.
does anyone out there know how to initiate code the instant a new file arrives in a folder?
does anyone out there know how to initiate code the instant a new file arrives in a folder?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You'll need to monitor the folder ...
http://www.mentalis.org/apilist/FindFirstChangeNotification.shtml
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=41816&lngWId=1
http://www.mentalis.org/apilist/FindFirstChangeNotification.shtml
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=41816&lngWId=1
You need to use the APIs, FindFirstChangeNotificatio n, FindNextChangeNotification to monitor folders and files.
If you want your app to run continuously and monitor folders even when the user logs off you will need to create it as a service, using the NTService.ocx so that your thread stays alive after log off and automatically runs when the machine boots up.
If you want your app to run continuously and monitor folders even when the user logs off you will need to create it as a service, using the NTService.ocx so that your thread stays alive after log off and automatically runs when the machine boots up.
' Watcher Class
Declare Function FindFirstChangeNotificatio n Lib "kernel32" Alias "FindFirstChangeNotificati onA" (ByVal lpPathName As String, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Long) As Long
Declare Function FindNextChangeNotification Lib "kernel32" (ByVal hChangeHandle As Long) As Long
Declare Function FindCloseChangeNotificatio n Lib "kernel32" (ByVal hChangeHandle As Long) As Long
Declare Function WaitForSingleObject Lib "kernel32" Alias "WaitForSingleObject" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Const FILE_NOTIFY_CHANGE_LAST_WR ITE = 16
Const WAIT_OBJECT_0 = 0
Dim mhNotifyChangeLastWrite as Long
Public Sub Init(Path as String)
mhNotifyChangeLastWrite = FindFirstChangeNotificatio n(Path, 0, FILE_NOTIFY_CHANGE_LAST_WR ITE)
End Sub
Public Function CheckForChange() As Boolean
If WaitForSingleObject(mhNoti fyChangeLa stWrite , 0) = Then WAIT_OBJECT_0
CheckForChange = True
FindNextChangeNotification vhChangeObject
End If
End Sub
Private Sub Class_Terminate()
If mhNotifyChangeLastWrite <> 0 Then
FindCloseChangeNotificatio n mhNotifyChangeLastWrite
mhNotifyChangeLastWrite = 0
End If
End Sub
'my Form
Private moWatcher as Watcher
Private Sub Form_Load()
Set moWatcher = New Watcher
moWatcher.Init("c:\temp")
End Sub
Private Sub tmr_Timer()
If moWatcher.CheckForChange() Then
' Code to execute on change
End If
Anthony.
Declare Function FindFirstChangeNotificatio
Declare Function FindNextChangeNotification
Declare Function FindCloseChangeNotificatio
Declare Function WaitForSingleObject Lib "kernel32" Alias "WaitForSingleObject" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Const FILE_NOTIFY_CHANGE_LAST_WR
Const WAIT_OBJECT_0 = 0
Dim mhNotifyChangeLastWrite as Long
Public Sub Init(Path as String)
mhNotifyChangeLastWrite = FindFirstChangeNotificatio
End Sub
Public Function CheckForChange() As Boolean
If WaitForSingleObject(mhNoti
CheckForChange = True
FindNextChangeNotification
End If
End Sub
Private Sub Class_Terminate()
If mhNotifyChangeLastWrite <> 0 Then
FindCloseChangeNotificatio
mhNotifyChangeLastWrite = 0
End If
End Sub
'my Form
Private moWatcher as Watcher
Private Sub Form_Load()
Set moWatcher = New Watcher
moWatcher.Init("c:\temp")
End Sub
Private Sub tmr_Timer()
If moWatcher.CheckForChange()
' Code to execute on change
End If
Anthony.
I am not sure if this solution will be as ellaborate as the one just provided because it would probably be slower because it uses FSO, however there the FileExists that you could use which is apart of the FSO and you could use that to check whether the file exists or not and insert the code into a timer to constantly check and once it does exist you could disable the timer if you wanted and launch or trigger what ever you wanted.
http://juicystudio.com/tutorial/vb/files.asp
Here is an example in vb script :
http://www.sloppycode.net/Reference/FSO/Ref-79.html
http://juicystudio.com/tutorial/vb/files.asp
Here is an example in vb script :
http://www.sloppycode.net/Reference/FSO/Ref-79.html
ASKER
so is everything basicly using a timer and checking the status of the folder?
ASKER
now if you can help me run this as a service or an icon in the task bar that would be great... (joking)
but thanks for the great input
but thanks for the great input
AFAIK this can't be done, unless you use VB to load all files into some kind of array, then use a form.timer event to check the files in the folder vs. the array every minute (or whatever time period you prefer).
I'd post this Q in the Active Directory channel as a 20-pt link to this Q, and see if any of those experts can answer this better.
Hope this helps.
-Jim