Example Code

Deleting a Folder and All of Its Contents Including SubFolders and Files in LabVIEW

Code and Documents

Attachment

This example allows the user to select a parent directory and delete that directory, all of its subdirectories, and all of the files contained within those folders. Currently, the delete function in the File I/O->Advanced Palette will only delete a directory when it is completely empty.

This example drills down into the folder as far as it can go and then deletes that folder and all of its contents. It uses shift registers to change the directory path for each iteration of the loop. It then works its way up the tree deleting each folder until it reaches and deletes the parent folder.

One particular example of when this would come in handy is if you want to copy over a folder and all of its subfolders and files each time you run your VI. Currently, there is no easy way to do this because if you try to copy a folder and its contents to an already existing folder, it just creates a subfolder underneath the existing folder. By using this example, you could delete the existing folder and then copy the files to that location.

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Contributors