Reputation: 319
I had an outside company write a "file watcher" program in C#. The program reads an excel sheet that contains a list of file paths of folders to watch. The "file watcher" program watches to see if any new text (.txt) files appear in each of the folders that it is watching. If there is a new text file, it opens an excel sheet, which contains a macro and runs the macro and then closes the sheet. Everything works great... Except... The text files are being automatically generated from a CMM (Coordinate Measuring Machine).
Side Note: Although, I have had a few programming classes (Python, Matlab, VBA for Excel). I am by no means a programming expert, nor a computer expert...
The CMM seems to create an empty .txt file (It shows up as 0 kb) then the CMM appears to dump some data every couple minutes. As a result, the text file will show 7 kb, then 15 kb... etc... I can click on this text file at any of these stages and it will open but only shows the text proportional to the kb of completion. If I close the text file and then re-open it, there is usually more completed. The CMM (if you aren't familiar) is measuring parts and reporting the measurements via these text files. Some parts take 15 seconds to measure (only a few measurements), some take 45 minutes (Many, many, measurements). Although I haven't confirmed this, there is a "Yes" / "No" dialog box that pops up for the CMM operator to click when the part is finished being measured and I think it doesn't finish writing the last chunk of the text file until the operator make a "Yes" or "No" selection. The operator could make this selection in 10 seconds, or 30 minutes (if they go to lunch or something). In fact, on a Friday, they could go home and not make a selection until Monday.
The problem I am having is that the C# program sends the file path of the text file to the excel macro before the text file is finish. In my mind, I need the C# program modified to "wait" for the text file to be complete before sending the file path of the txt file to the excel macro. Is this possible? Do text file contain like a EOF (End of File) marker of some sort?
Upvotes: 0
Views: 524
Reputation: 319
Here is the code i found to get me by...
Dim cmdLine As Object
Dim result As String
Dim SearchStr As String
Dim FilePath As String
Set cmdLine = CreateObject("WScript.Shell")
result = cmdLine.Exec("%comspec% /C Find " & SearchStr & " " & Chr(34) & FilePath & Chr(34)).STDOut.ReadAll
Upvotes: 0
Reputation: 769
If you cannot use any of the tricks in the comments, you should read the file contents into your C# program and save it as Excel or as a different text file only if "End of Report" is detected. If not, simply start over one minute later.
Maybe (I'm not sure how complicated this would be) you can read the file as a stream and receive the fresh data as soon as it is written. So you can wait for the file to complete and you don't have to read it over and over again.
Upvotes: 1