Link to home
Start Free TrialLog in
Avatar of icredes
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?
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Hi icredes,

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
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
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
Avatar of RainUK
RainUK

You need to use the APIs, FindFirstChangeNotification, 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.
' Watcher Class
Declare Function FindFirstChangeNotification Lib "kernel32" Alias "FindFirstChangeNotificationA" (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 FindCloseChangeNotification 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_WRITE = 16

Const WAIT_OBJECT_0 = 0

Dim mhNotifyChangeLastWrite as Long

Public Sub Init(Path as String)
mhNotifyChangeLastWrite = FindFirstChangeNotification(Path, 0, FILE_NOTIFY_CHANGE_LAST_WRITE)
End Sub

Public Function CheckForChange() As Boolean

    If WaitForSingleObject(mhNotifyChangeLastWrite , 0) = Then WAIT_OBJECT_0
       
        CheckForChange = True        
        FindNextChangeNotification vhChangeObject
       
    End If

End Sub

Private Sub Class_Terminate()
    If mhNotifyChangeLastWrite <> 0 Then
        FindCloseChangeNotification 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.
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
Avatar of icredes

ASKER

so is everything basicly using a timer and checking the status of the folder?
Avatar of icredes

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