[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

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?
1 Solution
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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.
Maybe this example can help:

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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 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
End Sub

Private Sub tmr_Timer()
If moWatcher.CheckForChange() Then
' Code to execute on change
End If

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.


Here is an example in vb script :

icredesAuthor Commented:
so is everything basicly using a timer and checking the status of the folder?
icredesAuthor Commented:
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now