PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 30, 2022

[FIXED] How to determine when a text (.txt) file has finished being written to disk using a file watcher program

 October 30, 2022     c#, eof, excel, file-watcher, vba     No comments   

Issue

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?


Solution

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


Answered By - XCELLGUY
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing