John
John

Reputation: 785

Checking for new files in a folder

I need to monitor a folder to see when new files are created and then have the file processed and then archived.

Its the actual detecting of new files i'm struggling with...I understand that I need to be looking at the FileSystemWatcher thing, but was wondering if anyone knows of any examples of its usage in this way to get me started?

Say my folder is "C:\Temp\", I need to know as soon as any file with a ".dat" extension appear.

Sorry for the vague question, I just havent been able to find what I'm looking for with various google searches.

Thanks in advance

Upvotes: 2

Views: 17029

Answers (2)

user3972104
user3972104

Reputation:

You can use FileSystemWatcher Class for this: It Listens to the file system change notifications and raises events when a directory, or file in a directory, changes.

Imports System
Imports System.IO
Imports System.Diagnostics

Public watchfolder As FileSystemWatcher

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    watchfolder = New System.IO.FileSystemWatcher()
    watchfolder.Path = "d:\pdf_record\"
    watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
    watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName
    watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Attributes
    AddHandler watchfolder.Changed, AddressOf logchange
    AddHandler watchfolder.Created, AddressOf logchange
    AddHandler watchfolder.Deleted, AddressOf logchange
    AddHandler watchfolder.Renamed, AddressOf logrename
    watchfolder.EnableRaisingEvents = True
End Sub


Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
    If e.ChangeType = IO.WatcherChangeTypes.Changed Then
        MsgBox("File " & e.FullPath & " has been modified" & vbCrLf)
    End If
    If e.ChangeType = IO.WatcherChangeTypes.Created Then
        MsgBox("File " & e.FullPath & " has been created" & vbCrLf)
    End If
    If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
        MsgBox("File " & e.FullPath & " has been deleted" & vbCrLf)
    End If
End Sub

Public Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)
    MsgBox("File" & e.OldName & " has been renamed to " & e.Name & vbCrLf)
End Sub

Upvotes: 4

John
John

Reputation: 785

So I have managed to get this working how I wanted and figured I'd share it incase anyone is ever after the same thing.

Using this guide [http://www.dreamincode.net/forums/topic/150149-using-filesystemwatcher-in-vbnet/] as a reference, I've added a FileSystemWatcher component to my form.

I use the following to hardcode the directory I want to monitor:

    Public Sub agent_Shown(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Shown
        Fsw1.Path = "C:\temp"
    End Sub

I use the following to add the full path of files created to a listbox...

Private Sub fsw1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles Fsw1.Created
    listbox_PendingJobs.Items.Add(e.FullPath.ToString)
End Sub

This works exactly how I want in terms of detecting new files in a folder. Now i'm going to drop a background worker in which is kicked off by a timer at 5 minute intervals to work through and 'process' the entries in the listbox if found.

Upvotes: 1

Related Questions