File System Watcher

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

File System Watcher

Post by jstevens »

I'm having a challenge with the following code "Sub watcher_Created..." The error message is: User-defined type not defined. My OS is Windows 11 64 bit.

I understand that System.IO should be selected in the VBA Project Tools>References. I don't see that option but the closest reference I see is "System". Set ref = ThisWorkbook.VBProject.References.AddFromFile("C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.tlb")

Code: Select all

'This code in a class module
Private WithEvents watcher As FileSystemWatcher


'This code in a standard module
Sub MonitorDirectory()
    Dim path As String
    path = "C:\Temp\DataFiles\"
    Set watcher = New FileSystemWatcher
    watcher.path = path
    watcher.NotifyFilter = (NotifyFilters.fileName Or NotifyFilters.LastWrite)
    watcher.Filter = "*.csv"
    watcher.EnableRaisingEvents = True
End Sub

Sub watcher_Created(ByVal source As Object, ByVal e As FileSystemEventArgs)   'This is where the challenge is
    ' Called when a new file is created in the monitored directory
    ' Update the corresponding checkbox in the ListView control
    Dim fileName As String
    fileName = e.Name
    ' Find the corresponding item in the ListView control
    Dim item As listItem
    For Each item In UserForm1.ListView1.ListItems
        If item.ListSubItems(1).Text = fileName Then
            item.Checked = True
            Exit For
        End If
    Next item
End Sub
Your suggestions are appreciated.
Regards,
John

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: File System Watcher

Post by HansV »

FileSystemWatcher is not a COM object (it's not listed in HKEY_CLASSES_ROOT\TypeLib), so you cannot create it. It is a .NET class.
I hope that someone else can suggest an alternative.
Best wishes,
Hans

snb
4StarLounger
Posts: 574
Joined: 14 Nov 2012, 16:06

Re: File System Watcher

Post by snb »

Apparently the Listview already contains an item with the name of the newly created file.
To check whether a new file has been created in a directory you can use the "scripting.filesystemobject" in the scripting library.
Alternatively you can check every listview.listitem for its existence using Dir(Path & listitem).

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: File System Watcher

Post by jstevens »

snb,

The FileSystemWatcher would run silently in the background as various processes were run simultaneously; each process generating a CSV file. The corresponding checkbox in the Listview would be checked once the file showed up in a folder. Think of using the Listview as a report indicating which files completed. The processing time varies so having the FileSystemWatcher run silently makes sense.

I don't believe the "scripting.filesystemobject" approach can run silently in the background. Using this approach I would not be able have the processes run simultaneously.

If I'm missing something, kindly let me know.

I tried capturing the ProcessID of each, wait for the ProcessID to complete and then update the corresponding check box as an alternative method. However having something run in the background such as the FileSystemWatcher made more sense.
Regards,
John

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: File System Watcher

Post by SpeakEasy »

In answer to your original question ...

1) Use Visual Studio to wrap FileSystemWatcher ina COM interop, and then y9u can use it in VBA just like a normal COM class. Some learning inmvolved, if you don't already know VB.NET or C # or Visual Studio

2) FileSystemWatcher is really just a .net wrapper around the Win32 ReadDirectoryChangesW api call - which can be used by VBA. But there's a fair amount of work to be done to get it running in the background properly.

3) "scripting.filesystemobject" solution - really just polling a folder - can in fact be written to 'run in the background', e.g by using a Timer

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: File System Watcher

Post by jstevens »

SpeakEasy,

Thanks for the suggestions. I’ll start with using a timer around "scripting.filesystemobject”
Regards,
John

snb
4StarLounger
Posts: 574
Joined: 14 Nov 2012, 16:06

Re: File System Watcher

Post by snb »

Example
You do not have the required permissions to view the files attached to this post.

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: File System Watcher

Post by jstevens »

snb,

Thank you for attaching the sample file. I was able to achieve my requirements based on what you provided.

I learned something new today. :clapping:
Regards,
John