Watching a folder for the existence of new CSV files RRS feed

  • Question

  • I have an application which will evaluate a folder path and determine if that path has new CSV files in it.  What should happen is the FileWatcher needs to recognize that a new file is being a created or has been created since the last time it has run and then pass this event to my file transporter which will phase the CSV for import into my database.  I have seen a number of examples relating to how the FileWatcher class works but I have not been successful in getting the class to catch a new file.  I think the reason it has not been successful is that it was not running at the time of the file addition. How do I keep the FileWatcher running so that it can catch the file and how do I prevent the code from attempting to process a file which may still be open when it is caught? 
    Thursday, August 23, 2018 2:14 PM


  • Hi

    I have often had strange issues with FileWatcher (I believe others have similar issues too).

    Here is one example to check for Files added/removed (of FileType) in a Folder (and sub folders if needed). The ProcessNewFiles Sub is where you would deal with files as added (of found when first run).

    This example silently deletes the record for any files which have been removed from the watched folder(s), but could easily be made to call a Sub for deleted files.

    ' Form1 with Timer1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim StorageData As String = Application.StartupPath & "\Data\Data.txt"
      Dim PathToWatch As String = "C:\Users\lesha\Desktop\Test"
      Dim files As New List(Of String)
      Dim TypeToWatch As String = "*.png"
      Dim NewFiles As New List(Of String)
      Dim AllFiles As New List(Of String)
      Dim SearchOpt As IO.SearchOption = IO.SearchOption.AllDirectories
      Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        IO.File.WriteAllLines(StorageData, files)
      End Sub
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If IO.File.Exists(StorageData) Then
          files = IO.File.ReadAllLines(StorageData).ToList
          IO.File.WriteAllLines(StorageData, files)
        End If
        With Timer1
          .Interval = 200
          .Enabled = True
        End With
      End Sub
      Sub CheckFiles()
        AllFiles = IO.Directory.GetFiles(PathToWatch, TypeToWatch, SearchOpt).ToList
        For i As Integer = files.Count - 1 To 0 Step -1
          If Not AllFiles.Contains(files(i)) Then
          End If
        For i As Integer = AllFiles.Count - 1 To 0 Step -1
          If Not files.Contains(AllFiles(i)) Then
          End If
      End Sub
      Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If NewFiles.Count > 0 Then
        End If
      End Sub
      Sub ProcessNewFiles()
        ' Process New Files Here
        ' I am just showing the Message
        MessageBox.Show("There are " & NewFiles.Count.ToString & " New Files found")
        ' Ensure NewFiles List is Empty after Processing
      End Sub
    End Class

    Regards Les, Livingston, Scotland

    • Marked as answer by Cam Evenson Thursday, August 23, 2018 5:36 PM
    Thursday, August 23, 2018 3:38 PM