473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

I am using the FilesystemWatcher 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

Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.IO.File
  3. Imports System.IO
  4. Imports System.Threading
  5. Imports System.Collections
  6. Imports System.Collections.Specialized
  7. Imports System.Diagnostics
  8.  
  9. Public Class FileSystemMonitor
  10.  
  11.     Public Shared Sub Main()
  12.         Dim fsw As New FileSystemWatcher()  ' create an object of FileSystemWatcher
  13.  
  14.         ' set properties of FileSystemWatcher object
  15.         fsw.Path = "c:\Test"
  16.         fsw.IncludeSubdirectories = True
  17.  
  18.         ' add event handlers 
  19.         AddHandler fsw.Created, New FileSystemEventHandler(AddressOf File_Created)
  20.  
  21.         fsw.EnableRaisingEvents = True  ' enable monitoring
  22.  
  23.         Console.WriteLine("Started Monitoring WinServ Dir. Press enter key to stop.")
  24.         Console.ReadLine()
  25.  
  26.     End Sub
  27.  
  28.     ' event handler to handle created event 
  29.     Public Shared Sub File_Created(ByVal obj As Object, ByVal e As FileSystemEventArgs)
  30.  
  31.         Dim fName As String = System.IO.Path.GetFileName(e.FullPath) 'file name with extension
  32.         Dim newPath As String = "C:\Test" 'Newpath with no file
  33.         Dim nPathAndFile As String = String.Concat(newPath, fName) 'Newpath with file and ext
  34.         Dim S As New StringCollection()
  35.         Dim T() As String = {nPathAndFile}
  36.         Dim i As String
  37.         Dim Count As Integer = 0
  38.  
  39.         For Each i In T
  40.             Count += 1
  41.             Dim di = New DirectoryInfo(newPath)
  42.             Dim fi = di.GetFiles("*", SearchOption.AllDirectories)
  43.             Console.WriteLine(fi.Length.ToString) ' & " " & nPathAndFile)
  44.  
  45.         Next
  46.  
  47.  
  48.  
  49.  
  50.         'If File.Exists(nPathAndFile) Then
  51.         'Console.WriteLine("Starting Transfer")
  52.         'Dim psi As New System.Diagnostics.ProcessStartInfo("C:\ftp2.bat")
  53.         'psi.RedirectStandardOutput = True
  54.         'psi.WindowStyle = ProcessWindowStyle.Hidden
  55.         'psi.UseShellExecute = False
  56.         'Dim ftp As System.Diagnostics.Process
  57.         'ftp = System.Diagnostics.Process.Start(psi)
  58.         'Dim myOutput As System.IO.StreamReader = ftp.StandardOutput
  59.         'Console.WriteLine(myOutput)
  60.         'ftp.WaitForExit(1000)
  61.         'If ftp.HasExited Then
  62.         'Console.WriteLine("Transfer Done")
  63.         'Dim s As String
  64.         'For Each s In System.IO.Directory.GetFiles("C:\Test")
  65.         'System.IO.File.Delete(s)
  66.         'Next s
  67.         'End If
  68.         'For Each i In T
  69.         'Delete(i)
  70.         'Thread.Sleep(2000)
  71.         'Next
  72.         'End If
  73.     End Sub
  74. End Class
  75. 'Dim delt As New System.Diagnostics.ProcessStartInfo("C:\delt.bat")
  76. 'delt.RedirectStandardOutput = True
  77. 'delt.WindowStyle = ProcessWindowStyle.Hidden
  78. 'delt.UseShellExecute = False
  79. 'Dim delt2 As System.Diagnostics.Process
  80. 'delt2 = System.Diagnostics.Process.Start(delt)
May 26 '10 #1
3 3793
tlhintoq
3,525 Expert 2GB
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 FileSystemWatcher 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.
May 26 '10 #2
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
May 26 '10 #3
tlhintoq
3,525 Expert 2GB
It's really not that tricky. Your queue can be an actual 'queue' object.
http://msdn.microsoft.com/en-us/library/7977ey2c.aspx
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.ModifiedDate 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.
May 26 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Anand K Rayudu | last post by:
Hi all, I am trying to find a way to get the files recursively in a given directory, The following code is failing, can some one please suggest what could be problem here from os import...
2
by: Matt | last post by:
Hello, I am writing a script that opens needs to get a listing of files in a directory, print that listing to a file and use that file as a quasi ftp control file. My problem is that when I...
2
by: Amy L. | last post by:
I am working on some code that will be used in a Windows Service that will monitor specific files in a queue. I would like to get an integer value of the amount of specfic files in a directory. ...
2
by: 73blazer | last post by:
Hello, I'm writing some C++ code, and I need to be able to find the number of files in a given directory. Is it possible under AIX4.3.3 with C++ 3.6.4? I cannot seem to locate anything of this...
6
by: Christopher | last post by:
What is the syntax for including files from another directory? In my case I want to include a file called "error.h" which lies in a directory called "support" which is a directory inside the...
1
by: Mink | last post by:
I have a code that checks a directory to see if it exists, and then if it does not, the code creates the directory and allows the user to add/view files in the directory. Weekly I print a report...
5
by: Robizzle | last post by:
I'm trying to write a simple console app that will rename all the files in a directory according to any rules supplied by a user during runtime. For example rename all *.jpg to *.jpeg. My problem...
3
by: | last post by:
I am a novice in C# and need help. I want to write a simple program to read a bunch of files from a specified directory and rename those files in a sequential fashion., changing a bunch of image...
1
by: jo3c | last post by:
hi everybody im a newbie in python, i have a question how do u parse a bunch of text files in a directory? directory: /dir files: H20080101.txt ,...
5
by: pavanponnapalli | last post by:
hi, I have got a list of files in a "list" as follows: foreach(@arr) { copy ("$_", "/home/pavan/tmp") or die "copy failed: $! "; } The path where the files...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.