From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I create a file pathname

How do I implment the common behavior in Win32 apps of creating the necessary path (i.e. folders) for a given pathfilename, if the path doesn't already exist? Is there C code anywhere that will do this?
0 Kudos
Message 1 of 5
(3,229 Views)
If the operator has to select a directory on disk, the FileSelectPopup or DirSelectPopup functions can help him to do this. Via these functions is also possible to create a directory, regardless of the state of the "Allow make directory" parameter (right click in the file list and choose New >> Folder).

On the other hand, if you have a pathname and you want to be sure that all directories exist, you can first use the SetDir with the full pathname and if it fails, recursively go back one directory at a time in the pathname (SplitPath + SetDir) until you find an existing directory and then create all the necessary directories to build the full pathname you need using the MakeDir function in the Utility library.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 5
(3,229 Views)
Thanks for the response, Robert. I realize that it can be done this way, but since that's a LOT of work, I was hoping someone had a code snippet or routine somewhere with this functionality implemented, since it's so commonly used. I can't find anything built up in Win32 SDK that will do it either. I must have seen a hundred apps that implement this ("Path doesn't exist, create path?") and thought that there might be something out there.

Thanks again, Robert, I appreciate it.

Bob Hayes
0 Kudos
Message 3 of 5
(3,229 Views)
As Roberto said, you can do some recursive work with MakeDir. (MakeDir can only create one directory at a time). I use GetFileAttrs() instead of SetDir() to check the directory.
Here's a sample CVI 6.0 project that does that. The real work happens in FileUtils.c. CheckPath.c and .uir are just there to get FileUtils the path and to report status. If you include FileUtils.c in your project and #include "FileUtils.h" in your main .c file, you can call CheckFileDirectory() to check the file directory and to create it (including subdirectories) if needed.
0 Kudos
Message 4 of 5
(3,229 Views)
ALS -

Thanks! I'll look at that code. Yup, recursion should work so long as we have an end condition.

Bob Hayes
0 Kudos
Message 5 of 5
(3,229 Views)