08-06-2021 09:29 AM
I am learning how to watch a folder for new files. I need to test new files written for certain attributes.
I have the attached example from NI that watches a folder for new files. The example that NI provides retrieves the filename with the base path. I need the entire base path and filename so I can load it in my callback and process it.
The bottom line is I do not understand how the watcher and callback are working together to get the filename.
It looks to me like the watcher "sees" that a new file is saved and calls the callback and passes parameters. From there I am completely confused as to how the two vi's get what data and who passes what to who.
How would I pass a cluster of data to the callback to be used by the callback?
08-06-2021 12:42 PM
In this bit of code here:
The thing there that says "New file name" on it is a reference to the control named that on the front panel of the VI. Then in the callback VI, there is this code:
That takes the name of the changed file from the event data, then uses the reference to that control to update the value shown on the front panel of the original calling VI. If you have ever done C/C++ programming, it's as if it passed a string pointer.
This whole thing is done using .NET events, which are sort of "outside" of LabVIEW, so you can't just get the value directly back, you have to use some sort of "messaging" to get it back, because the .NET event could occur zero times, one time, or a thousand times, so you can't just have a simple output somewhere.
Do you know how to use queues or user events in LabVIEW? If not, I suggest you look them up and what they do, and then edit the callback VI to send the file name as a user event or a queue element, and then have another part of your code get those events or queues and use those to start the file checks you need to do.
Another thing you could do that might be easier to get started with (but NOT better in the long run) would be to change the "New file name" indicator into a control instead, switch the "Value" property node in the callback VI with a "Value (Signaling)" node, and then in the While loop of the main VI that is now empty, put an event structure in there and create a frame that looks for a "Value change" event on the "New file name" string control, and then use that as the trigger for your code.
Note: Because you're using a callback VI with .NET code, you might notice that the callback VI doesn't leave the running state when your code is done. If so, look at this thread:
There are notes and instructions in there on how to set up a GC.Collect node to fix the problem.
08-06-2021 02:46 PM
Kyle97330 - I was familiar with queues. I did not realize that they were "system wide" and that you can obtain a queue by name in multiple VI's and it's still the same queue. Things you learn. That certainly works.
08-06-2021 04:41 PM
By the way, if you want to use a queue by reference, you can. You can unwire the reference to the string control that's passed in, then wire in a queue reference instead. This will mean the callback VI won't work any more, but if you unwire the callback VI, then right-click the "Register" node, there will be an option in the menu to create a callback VI, and when created it will have the queue reference that you passed into it as an input. You'd use the same unbundle and such to get the name of the changed file from .NET, but then you could send the file over the queue reference without needing to create the reference using the same named queue if you prefer to pass it by reference.
08-08-2021 11:25 AM
I am using an example that uses .NET and reports the name of files that were added/created to a directory. I want to copy those files to a different directory, but I don't know how to do that.
I attached the watch directory vi and the callback event.
Thanks
Avi.
08-08-2021 12:28 PM
If you already have the source file paths and their destinations, there's a File Copy function under Advance File functions palette.
Sorry couldn't show a snippet, posting from mobile.
08-09-2021 02:17 AM
Thanks, but since it is retrieved by a .NET callback I don't actually have the filename. If I understand how it works, it is kind of outside LV.