How to monitor a folder for change in vb6.0

Posted on 2004-11-18
Last Modified: 2008-02-07
I am looking to be pointed in the right direction.
We need to monitor a folder on our server and when there is a new pdf file put into that folder we need to pull the file name as a string and compare that to another string.
I need to know how to have a dll monitor the folder and pull the file name out of the folder?

More info. The client will pass a string to the dll, the dll will watch the folder for 60 seconds, and if in 60 secods there is a change in the folder it will pull the name of the file put into the folder?

Question by:shayne23d
    LVL 32

    Assisted Solution

    See this example:

    Creating a Watched Folder with FindChangeNotification
    LVL 17

    Accepted Solution

    Here is an example which runs on a web-site some users are allowed to upload files. When a new file arrives the software detects the arrival an does something.

    I found the examples on the internet a little bit unsuitable so I created an a version that just used a time control.

    the example also uses a list box lstActions

    The coe uses mlAction to decide what it is currently doing.

    1) So first it sets a waiting for change which is the the form of a process which has a handle
    It connects to the process via the handle
    2) when a change notification arrives it closes the wating for a change handle and runs the process
    then it sets the flag to tell the timer to restart the game

    Option Explicit

    Dim mlAction As Long

    'Monitoring file changes
    ' borowed concepts from:
    'A popular function developers are implementing these days consists of monitoring a system for changes to files. This feature can be used to detect situations where viruses modify your system files, or web sites add cookies and other files to your system. It can also be used to notify you that a shared file has been updated and the changes need to be reviewed.
    'This article will discuss monitoring for file changes in Visual Basic 6.0 using the FindFirstChangeNotification API function. The parameters passed to the function describe the folder you want to monitor, whether to monitor the folder's subfolders, and the type of file changes to detect. You can also use the FindNextChangeNotification API function to continue monitoring the folder after you have detected a file change, and the FindCloseChangeNotification API function to stop monitoring the folder. The sample code below demonstrates monitoring the Windows folder for file changes.
    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 FindCloseChangeNotification Lib "kernel32" _
       (ByVal hChangeHandle As Long) As Long
    Private Declare Function FindNextChangeNotification Lib "kernel32" _
       (ByVal hChangeHandle As Long) As Long
    Private Declare Function WaitForSingleObject Lib "kernel32" _
            (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

    Dim FOP As New zFOP
    Dim GF As New zGF

    Private Sub zProcessChange(psFolder As String)

    ' run all batch files in the folder

    Dim Files
    Dim vFile

    ' get the files in the folder
    Set Files = FOP.GetFiles(psFolder + "\*.bat", True)
    If Files Is Nothing Then Exit Sub
    Dim OK

    For Each vFile In Files
        ' process ach file
        zStatus "Found: " + vFile
        ' wait until complete
        OK = GF.RunCommandOK(psFolder + "\" + vFile, vbNormal)
        'now kill the file
        OK = GF.KillFileOK(psFolder + "\" + vFile)

    End Sub

    Public Sub zStatus(psStatus As String)

    ' Display Status

    lstActions.AddItem Format(Now, "dd-mm-yy hh:nn:ss") + " " + psStatus, 0

    If lstActions.ListCount > 100 Then
        lstActions.RemoveItem lstActions.ListCount - 1
    End If

    End Sub

    Private Sub Form_Load()
    mlAction = 0
    timStartWaiting.Interval = 100
    Dim sh, sw
    If GF.Minimized(Me, sw, sh) Then Exit Sub
    lstActions.Move 0, 0, sw, sh

    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    mlAction = 9
    timStartWaiting.Interval = 0
    End Sub

    Private Sub lstActions_Click()

    End Sub

    Private Sub timStartWaiting_Timer()

    Dim sFolder As String
    sFolder = "C:\WINNT\System32\Logfiles\dns"

    Static lWaitHandle As Long
    Static lResult As Long

    ' Action = 0 means start waiting
    If mlAction = 0 Then
        zStatus "Waiting"
        timStartWaiting.Interval = 0
        lWaitHandle = FindFirstChangeNotification(sFolder, _

       mlAction = 1
    End If

    ' could have used this but chose not to
    'r = FindNextChangeNotification(hWatched)

    If mlAction = 1 Then

        lResult = WaitForSingleObject(lWaitHandle, 50)
        If lResult = 0 Then
            timStartWaiting.Interval = 0
            mlAction = 0
            zStatus "Change notified"
            FindCloseChangeNotification lWaitHandle
            ' DO THE THING
            zProcessChange sFolder
            'FindNextChangeNotification (hWatched)
            timStartWaiting.Interval = 100
            ' folder has changed
            timStartWaiting.Interval = 100
        End If
    End If

    If mlAction = 9 Then
        FindCloseChangeNotification lWaitHandle
        timStartWaiting.Interval = 0
        mlAction = 99
        zStatus "Closed"
    End If

       'flag requests detection of file name changes including renaming, creating, or deleting a file name.
       'The FILE_NOTIFY_CHANGE_LAST_WRITE flag requests notification when a file'
       'is written to the disk. The FILE_NOTIFY_CHANGE_ATTRIBUTES flag requests notification when
       'a file's attributes have changed (such as resetting the file's ReadOnly attribute).
       'Other file change flags available for use include FILE_NOTIFY_CHANGE_DIR_NAME'
       'which detects folder name changes, FILE_NOTIFY_CHANGE_SIZE which detects changes
       'in the file's size (this is covered by the FILE_NOTIFY_CHANGE_LAST_WRITE flag),
       'and FILE_NOTIFY_CHANGE_SECURITY which detects changes to a file's security descriptor.
    End Sub


    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Macro which simplifies accounts 6 49
    VBA - Excel progress bar 22 72
    Adding to a VBA? 6 34
    Run code from text file in vb 1 18
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    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…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    754 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

    19 Experts available now in Live!

    Get 1:1 Help Now