LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Warning on recursing through a Directory Tree

I just got bitten by an unusual feature regarding traversing Directory Trees recursively.  In particular, I wanted to process all of the Folders from a Root Path, so I set up the following simple recursion:  To Process a Folder, (a) Do whatever you need to do on the Folder, (b) get a list of all Folders in this Folder, and (c) (recursively) Process a Folder on each of the Folders in Step (b).

 

I actually implemented this using a Reentrant VI, but you could also do this iteratively, adding folders onto an Array on a Shift register and removing them when they are processed.

 

I used the function List Folder in Step b, as this returns an array of Folders found in the specified Folder (sorry for the echo-chamber effect).  I recently ran this on a pretty big directory tree that included a copy of a C: drive, with Windows stuff, and noticed some weird and unexpected files showing up.

 

The problem is that what List Folder considers a Folder includes Shortcuts.  These are interpreted in the current context, so a shortcut to C:, for example, will actually point to your C: drive, and the recursive process will then start walking down your C: drive, probably not what you want to happen.

 

The fix (once you realize there is a problem, hence the rationale for this note) is to put a File/Directory Info function before you do any processing of a "folder" (which might be a Shortcut).  Use the "Shortcut" Boolean output and a Case Statement around the rest of the "folder-processing" code and make the True case a "Do Nothing", with a Label saying "Shortcut, do not process!" (the False case is your normal Folder processing code).

 

It turns out that NI does know about this -- they have a function called Recursive File List that recursively lists both files and folders (iteratively), and buried inside there is a similar "Ignore Shortcut" Case Statement.  Live and Learn.

 

Bob Schor

Message 1 of 1
(2,419 Views)