Showing results for 
Search instead for 
Did you mean: 

execute vi when a certain file is in a certain directory

I have a VI which read and converts a bmp into a  2D array. Now a want that the VI does this automaticely when  a new bmp is inserted by another external application in this directory. I don't know how to do this.
Thanks for your help
Using Labview 7.1
0 Kudos
Message 1 of 9
Hello Simon,

there no such option (AFAIK) to detect this automatically. You have to write a VI polls that directory every N ms.
There may be an windows event (.NET?) that can help you, but therefore you first have to find such an event.

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep
My LabVIEW Ideas

LabVIEW, programming like it should be!
Message 2 of 9

There is indeed a .NET assembly which can be set to watch a directory for new files to appear. You can register for a .NET callback event if a file is created, renamed, deleted, etc. You can also explicitly set a filter to use. By that I mean you can set it to fire the event for only *.bmp files and not others. The .NET class in question is called FileSystemWatcher.

Take a look at the example below. Just enter in a path and a filter to use (such as *.txt or *.bmp), then run the example. Whenever you add a file to that folder that matches the filter, a string control value will update with the new file name. You will have to know something about .NET callback event handling and .NET use in general to use this in LabVIEW, but if you take the time to learn it, you'll get a much better solution than polling (which isn't a bad thing to fall back on either 🙂 ).

Here are some links on .NET and .NET events in LabVIEW. Hope this helps!

Jarrod S.
National Instruments
Message 3 of 9

hi there

what about files copied to the directory when the LV-app is NOT running? can the .NET event handle the case when several files are copied to the directory at the same time while the processing of the first file is still going on? if this could be the case i'd choose pooling

Best regards

CL(A)Dly bending G-Force with LabVIEW

famous last words: "oh my god, it is full of stars!"
0 Kudos
Message 4 of 9

.NET events registered by a LabVIEW VI can only be handled during run-time, just like any other events.You could of course create a service to run in the background running this .NET code. LabVIEW could then query the information from that service.

I don't see a particular need for that here. The best thing to do would be to poll once at the beginning for a first selection of files, then register the .NET event for all subsequent files.

I'm not terribly familiar with this class, so I'd refer you to the link above for the msdn website, but I'm pretty sure it can handle multiple files simultaneously. There are settings I didn't play with that allow for buffering of some sort. Have a look.

Jarrod S.
National Instruments
0 Kudos
Message 5 of 9

From the website linked above:

"The Windows operating system notifies your component of file changes in a buffer created by the FileSystemWatcher. If there are many changes in a short time, the buffer can overflow. This causes the component to lose track of changes in the directory, and it will only provide blanket notification. Increasing the size of the buffer with the InternalBufferSize property is expensive, as it comes from non-paged memory that cannot be swapped out to disk, so keep the buffer as small yet large enough to not miss any file change events."


Hope this helps!

Jarrod S.
National Instruments
0 Kudos
Message 6 of 9

hi, i met an  problem when i use this method to watch a directory.


there is indeed an event when i copied a file to the watched dir. but if the file is too large, there is also an event then the file is not copied completely. then there will be an error if i read this file.



0 Kudos
Message 7 of 9
It sounds like the event is fired before the copy operation is complete. You can probably work around this by polling the file size for a couple milliseconds after you get the event until the file stops growing. Not sure if there's a better way or not...
Jarrod S.
National Instruments
0 Kudos
Message 8 of 9

Thanks for your reply!

An error with code 8 occurred when getting the file size during the copy operation. Is there any way to do this based on the file permission? Thanks again! 

0 Kudos
Message 9 of 9