LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Monitor a folder with handler kind of structure

I'm trying to monitor an empty folder when a new *.txt file is created there (by another software), then my app processes the newly created *.txt file and move it away (archive it). The app requires to be on all the time, in order to process any new *.txt file created at any time.

 

I figured a while loop to constantly monitor the folder is wasting unnecessary CPU usage, hence I wonder is there any handler kind of structure that handles that easily?

 

 

0 Kudos
Message 1 of 6
(3,110 Views)

Well, you need to ask how promptly you need to process the new files, which (in turn) may be related to how often a new file appears.  Let's say that you want to process a file within a second of its appearance in your folder.  Simple, have a While Loop where you test the folder, and if there's a file, you process it, otherwise you wait 1000 msec (= 1 second).  If new files come, say, once a minute, then 59 out of 60 loops will be "Wait one second", and only a single loop will actually consume significant cycles processing your file.  Almost all of the CPU time will be "waiting", so the PC can do other things.

 

Bob Schor

0 Kudos
Message 2 of 6
(3,095 Views)

Thanks for the prompt respond Bob.

 

Unfortunately I don't know how often the new file(s) appears. That depends on the usage of users. Hence it can be several hundreds in few minutes, or a couple in a few hours. 

 

Part of my app prints out info from each new file while a user uses the software (which stays on indefinitely). Hence the app needs to be quite promptly too.

 

I was hoping LV has something that handles a folder, something like event structure in a loop that handles UI.

 

0 Kudos
Message 3 of 6
(3,091 Views)

Check this out!

 

http://forums.ni.com/t5/LabVIEW/How-to-trigger-an-event-when-a-file-is-created/m-p/2544613#M770708

----------------------------------------------------------------------------------------------------------------
Palanivel Thiruvenkadam | பழனிவேல் திருவெங்கடம்
LabVIEW™ Champion |Certified LabVIEW™ Architect |Certified TestStand Developer

Kidlin's Law -If you can write the problem down clearly then the matter is half solved.
-----------------------------------------------------------------------------------------------------------------
0 Kudos
Message 4 of 6
(3,086 Views)

The other suggestions posted here (with the possible exception of FileSystemWatcher, which appears to use a .NET function whose behavior I don't know) are variations on my suggestion to periodically monitor the folder for files to process.  Note that I argue (and you can write test code to verify this) that if there is nothing to do, my "monitoring process" will consume very little time.  Also, if properly constructed, the "Process files in folder" routine that is called when the monitor says "There's something to do!" can "process until the folder is empty".

 

Suppose files came in on average once every 10 seconds.  If you monitor once/second, you'll start processing within a second of a file's arrival.  Even if a "clump" comes in (say, five files), the worst that would happen is your routine would process all five before it went back to its "Wait for more to do" state.

 

If this routine is running as a parallel (asynchronous) loop, it will "share time" with the routines producing the data, and as long as the processing is not too "heavy-duty" and demanding of resources (and take "forever" to do) should be able to run in parallel with the rest of your code.

 

But don't take my word for it -- it is fairly easy to code this up, have a dummy "Processing" routine and write a dummy "Generate data files" routine to see how it works in your situation.

 

Bob Schor

0 Kudos
Message 5 of 6
(3,030 Views)

Sorry for late respond, but thanks Bob (and the NI representative that called me previous). I finally able to verify it recently by adding a 1000msec wait in the loop to constantly monitor a folder, and it doesn't chew up too much memory by doing it so too. So problem solved!

0 Kudos
Message 6 of 6
(2,946 Views)