How do I Wait for files to finish populating a directory before processing...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dapiechocki
    New Member
    • May 2010
    • 2

    How do I Wait for files to finish populating a directory before processing...

    I am using the FilesystemWatch er to look for files in a Directory, it does not matter what the file is. After a file appears it starts my ftp process and I end up with a bunch of 0 byte files because it processed before all the files arrived. I would like to WaitFor... (Files_Created) to finish but dont seem to have the right syntax to get this to work properly. I have working code up until it loops. As you can see I have tried various processes to get it to work... mostly commented out. I also have imported just about everything trying to get this to work. Please ask away. Thanks

    Code:
    Imports System
    Imports System.IO.File
    Imports System.IO
    Imports System.Threading
    Imports System.Collections
    Imports System.Collections.Specialized
    Imports System.Diagnostics
    
    Public Class FileSystemMonitor
    
        Public Shared Sub Main()
            Dim fsw As New FileSystemWatcher()  ' create an object of FileSystemWatcher
    
            ' set properties of FileSystemWatcher object
            fsw.Path = "c:\Test"
            fsw.IncludeSubdirectories = True
    
            ' add event handlers 
            AddHandler fsw.Created, New FileSystemEventHandler(AddressOf File_Created)
    
            fsw.EnableRaisingEvents = True  ' enable monitoring
    
            Console.WriteLine("Started Monitoring WinServ Dir. Press enter key to stop.")
            Console.ReadLine()
    
        End Sub
    
        ' event handler to handle created event 
        Public Shared Sub File_Created(ByVal obj As Object, ByVal e As FileSystemEventArgs)
    
            Dim fName As String = System.IO.Path.GetFileName(e.FullPath) 'file name with extension
            Dim newPath As String = "C:\Test" 'Newpath with no file
            Dim nPathAndFile As String = String.Concat(newPath, fName) 'Newpath with file and ext
            Dim S As New StringCollection()
            Dim T() As String = {nPathAndFile}
            Dim i As String
            Dim Count As Integer = 0
    
            For Each i In T
                Count += 1
                Dim di = New DirectoryInfo(newPath)
                Dim fi = di.GetFiles("*", SearchOption.AllDirectories)
                Console.WriteLine(fi.Length.ToString) ' & " " & nPathAndFile)
    
            Next
    
    
    
    
            'If File.Exists(nPathAndFile) Then
            'Console.WriteLine("Starting Transfer")
            'Dim psi As New System.Diagnostics.ProcessStartInfo("C:\ftp2.bat")
            'psi.RedirectStandardOutput = True
            'psi.WindowStyle = ProcessWindowStyle.Hidden
            'psi.UseShellExecute = False
            'Dim ftp As System.Diagnostics.Process
            'ftp = System.Diagnostics.Process.Start(psi)
            'Dim myOutput As System.IO.StreamReader = ftp.StandardOutput
            'Console.WriteLine(myOutput)
            'ftp.WaitForExit(1000)
            'If ftp.HasExited Then
            'Console.WriteLine("Transfer Done")
            'Dim s As String
            'For Each s In System.IO.Directory.GetFiles("C:\Test")
            'System.IO.File.Delete(s)
            'Next s
            'End If
            'For Each i In T
            'Delete(i)
            'Thread.Sleep(2000)
            'Next
            'End If
        End Sub
    End Class
    'Dim delt As New System.Diagnostics.ProcessStartInfo("C:\delt.bat")
    'delt.RedirectStandardOutput = True
    'delt.WindowStyle = ProcessWindowStyle.Hidden
    'delt.UseShellExecute = False
    'Dim delt2 As System.Diagnostics.Process
    'delt2 = System.Diagnostics.Process.Start(delt)
    Last edited by tlhintoq; May 26 '10, 06:09 PM. Reason: [code] your code here [/code] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Welcome to pain in the butt that is Windows. As soon as a new record is put into the file directory, windows fires a NewFileEvent. Just checking to see if the file exists won't cut it, because even at 0k it does technically exist.

    When using FileSystemWatch er I usually put the path of the new file into a Queue rather than react immediately. I can then handle the queue at me leisure; checking every 10 seconds to see if the file size has changed. Once the size has remained stable for 30 seconds I figure it must be done.

    Hopefully someone will pop up with a better approach and we can both learn something. But it has been a method that serves me well for a long time.

    Comment

    • dapiechocki
      New Member
      • May 2010
      • 2

      #3
      WOW, that sounds like a tricky process. You wouldnt happen to have any sample code laying around would you?

      The problem lies in FSW processing files in a batch but then wants to loop throuth the code for every file. I want it to sit and wait until there is no folder activity (Change or created events) happening before it goes to the ftp. I am not a VB expert and dont know mutch about multi-threading

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        It's really not that tricky. Your queue can be an actual 'queue' object.

        Or just a simple List<string> of the paths.
        Or make a class that has a path, size and date in it to give yourself some flexibility - then make a List<> of those to loop through.

        On a timer check the FileInfo.Modifi edDate of each file in the list (or the last known filesize). If it hasn't changed in 30 seconds, you're probably good.

        I code in C#, plus it's all proprietary to the company I work for... so I can't really just give it out.

        But give it a play. You'll learn more by doing and experimenting anyway.

        Comment

        Working...