trigger code on file copy

Posted on 2005-04-08
Last Modified: 2008-02-01
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?
Question by:icredes
    LVL 65

    Expert Comment

    by:Jim Horn
    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.
    LVL 53

    Accepted Solution

    Maybe this example can help:
    LVL 53

    Expert Comment

    LVL 5

    Expert Comment

    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.
    LVL 8

    Expert Comment

    ' 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

    LVL 23

    Expert Comment

    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 :
    LVL 1

    Author Comment

    so is everything basicly using a timer and checking the status of the folder?
    LVL 1

    Author Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now