Learn how to a build a cloud-first strategyRegister Now


How to monitor a folder for change in vb6.0

Posted on 2004-11-18
Medium Priority
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

Erick37 earned 1000 total points
ID: 12621691
See this example:

Creating a Watched Folder with FindChangeNotification
LVL 17

Accepted Solution

inthedark earned 1000 total points
ID: 12624703
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:
' http://www.desaware.com/tech/filemonitoring.aspx
'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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…
Suggested Courses

810 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