I have a simple file watcher application which watches the folder C:\. I am watching the folder so that as soon as a new file is created in C:\, it moves the newfile to a specified folder somewhere else. My problem is that if two files are dropped in the C:\ folder at the same time, my code only deals with one of the files. I would like my code to identify both files, and move both of them.

Here's some of my code:

Dim folderToWatch As String = "C:\"

PrvSubMain()

watchFolder = New System.IO.FileSystemWatcher()
watchFolder.Path = folderToWatch
watchFolder.NotifyFilter = IO.NotifyFilters.FileName
AddHandler watchFolder.Created, AddressOf FileCreated
watchFolder.EnableRaisingEvents = True

End Sub

Private Sub FileCreated(ByVal source As Object, ByVal e As _
System.IO.FileSystemEventArgs)

Dim fileName As String = ""
Dim pathOfFile As String

If e.ChangeType = IO.WatcherChangeTypes.Created Then
fileName = e.Name
End If

pathOfFile = e.FullPath

If fileName = "" = False Then

'Pass the pathOfFile and fileName off to the MoveFile function where it will move the file to another folder..
MoveFile(pathOfFile, fileName)
watchFolder.EnableRaisingEvents = False
End If

End Sub

Thankyou in advance!

Recommended Answers

All 3 Replies

>My problem is that if two files are dropped in the C:\ folder at the same time, my code only deals with one of the files.

Don't worry. Deleted event will be fire twice. Check the properties of and object of FileSystemEventArgs class in Deleted event.

Thanks adatapost, unfortunatly your solution didnt help remedy the problem. But i have figured it out now, i have created a new thread in order to keep the application running an the code goes round again and deals with the remaining files.

Thanks judge6. I am glad you got it working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.