Click here to Skip to main content
0 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I'm using the following code to monitor a folder using Filesystemwatch
Dim foldertowatch as New FileSystemWatcher()
foldertowatch.Path = source_folder

                With foldertowatch
                    'File type
                    .Filter = source_prefix & "*." & source_file_type
                    'Notify Filter
                    .NotifyFilter = .NotifyFilter Or NotifyFilters.FileName
                    .NotifyFilter = .NotifyFilter Or NotifyFilters.DirectoryName
                    'To watch sub directories....
                    .IncludeSubdirectories = sub_folder
                    'Handle new file created
                    AddHandler .Created, AddressOf Process

                    .EnableRaisingEvents = True
                End With


And using the below sub to handle the created event

Private Sub Process(ByVal Source As Object, ByVal evt As FileSystemEventArgs)
'To do...
End Sub


Now it comes to a point where I need to monitor multiple folders, each folder with different file types, and different processes need to do. For example

Source 1 - Monitor pdf file when created - To Do: Rename and save to Destination 1
Source 2 - Monitor txt file when created - To Do: Rename and save to Destination 2
Source 3 - Monitor excel file when created - To Do: Rename and save to Destination 3
and so on...

What I have tried:

I tried to created multi instances of Filesystemwatch

Dim foldertowatch(total_items) As FileSystemWatcher
        For i = 0 To 10
            foldertowatch(i) = New FileSystemWatcher()

            foldertowatch(i).Path = source(i)

                With foldertowatch(i)
                    'File type
                    .Filter = source_prefix(i) & "*." & source_file_type(i)
                    'Notify Filter
                    .NotifyFilter = .NotifyFilter Or NotifyFilters.FileName
                    .NotifyFilter = .NotifyFilter Or NotifyFilters.DirectoryName
                    'To watch sub directories....
                    .IncludeSubdirectories = sub_folder(i)
                    'Handle new file created
                    AddHandler .Created, AddressOf Process

                    .EnableRaisingEvents = True
                End With
        Next


But I don't know how to handle the events raised from different instances. Basically, How do I know which instance raises the event so that to proceed with rename/save to correct folder.
Posted
Updated 20-Mar-19 10:22am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The FileSystemEventArgs class has a file path and a name property. Use those to determine which file is affected. Add logic to determine the file type/path and do whatever you want to do with each file type or folder.

See also: FileSystemEventArgs on docs.microsoft.com
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190423.1 | Last Updated 20 Mar 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100