LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to check if the path exists and if not, create it

I'm trying to check if the path given by the user already exists. In case it doesn't, I'd like to create a folder with name given in the same directory.

Let's say path_1: "C:\folder1" and path_2: "C:\folder2", and suppose that folder1 exists but folder2 doesn't. Then if the path given by the user is the path_1, then ok, nothing happens, but if it is the path_2, then the programm should realize that it doesn't exist in C:\ and should ask you whether you want to create it or not.

I know how to do that for files, but not for folders!

 

Thanks in advance

0 Kudos
Message 1 of 12
(10,034 Views)

I can think of at least two options to perform this task:

1. Use SetDir to set the current working directory to the path specified by the user: the function returns -1 if the directory is not found, in which case you can create it.

2. Use MakeDir to directly create the stated directory: if it already exists the function returns -9. You may need to chek other return codes to warn the operator of other errors found.



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?
Message 2 of 12
(10,032 Views)
I'm not sure about SetDir, but I know MakeDir causes a library error when you do that, which means you'll break everytime you do that check if you debug with "Break On Library Errors" enabled.  The way I've done it is using GetFileAttrs(..) which returns a 1 if it's a directory, 0 if it's a file, and -1 if it doesn't exists, and does not cause a library error.
0 Kudos
Message 3 of 12
(10,029 Views)

You're right: I checked it while I had this function disabled... If you don't want to permanently disable this option, you can embed the instruction between two calls to SetBreakOnLibraryErrors that mask the error and restore original flag state:

 

prevBOLE = SetBreakOnLibraryErrors (0);

MakeDir (...);

SetBreakOnLibraryErrors (prevBOLE);



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?
Message 4 of 12
(10,027 Views)

You can just use GetFileAttrs from the Utility library (which returns 1 if the path is a directory, -1 if not found), or FileExists from the toolbox (which is actually just a thin wrapper for the GetFileSize Utility library function). The documentation doesn't mention that FileExists works for directories, but it does. Don't ask me what the value returned in the size parameter means though... 🙂

 

Mert A.

National Instruments

0 Kudos
Message 5 of 12
(10,001 Views)

Thank you for all repplies!

I've tried the SetDir and MakeDir before, but I didn't know how to break on library errors.

It was always suspending my execution and I didn't know how to deal with it.

It works great, thanks a lot.

0 Kudos
Message 6 of 12
(9,969 Views)

I have tried to use the GetFileAttrs(), but it didn't work, this function will popup LIB ERROR and break the project too.

0 Kudos
Message 7 of 12
(8,932 Views)

As Roberto posted earlier, before your call to GetFileAttrs, etc.,  just use SetBreakOnLibraryErrors(0) to disable breaking on library errors, then SetBreakOnLibraryErrors(1) to enable breaking after that.

 

Al S

0 Kudos
Message 8 of 12
(8,916 Views)

hi guys,

i`m new at this so im gonne prob. ask a noob`s question...

i have the same issue as in the title.

but when i run :

                                          int a,b,c,d,folderexist=-1;
                                         switch (event)
                                          {
                                                   case EVENT_COMMIT:
                                                                   {
                                                                             GetCtrlVal (panelHandle, PANEL_NAME, name);
                                                                             folderexist = GetFileAttrs (name, &a, &b, &c, &d);
                                                                             if (folderexist==-1)
                                                                              {
                                                                                       MakeDir (name);
                                                                                       sprintf...........

                                                                              }

                                                                             else.......

if the folder do exists it works perfectly.

if it doesnt - it breaks the run and gives me the message :

                      NON-FATAL RUN-TIME ERROR: "NewScap.c", line 317, col 27, thread id 0x00001B98: Library function error (return value == -1 [0xffffffff]).

(and mark the getfileattrs line in blue)

and when i check the "folderexits" value it shows 2!? not 1 not 0 not even -1, but 2...

can somone please help ?

thanks 

Adi.

 

0 Kudos
Message 9 of 12
(8,582 Views)

Tnx Alot !

i`ve use ur solution.

it helped...

just have to ask...do i need to use it everytime ?

why does the function GetFileAttrs works without SetBreakOnLibraryErrors...?

0 Kudos
Message 10 of 12
(8,563 Views)