LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Creating/replacing a file in a directory, without a prompt or error

I am attempting to do something fairly simple in LabView but it seems that there must be an easier or more elegant way than the way I am doing it. 
 
I have the main .vi running in c:\programs and it calls another .vi in the same directory.  In this subVI the program creates/replaces tags.dat in the c:\programs\data directory.  I want the file to be created if it doesn't exist or replaced if it does exist.   If the \data directory doesn't exist, I want it created.  And also I want the .vi to run in any directory and as a .exe or as a .vi.  This is what I am doing to accomplish this:
 
1) Use App.kind, "Current VI's Path" and a case statement to strip once if it is "development system" and twice if it is "run-time system".
2) Then use "LIST Directory" and a "For Loop" to look for "data" directory.  If it exists, do nothing, if it does not use "New Directory" to create it. 
3) Use "LIST Directory" and a "For Loop" to look for "tags.dat" in \data directory.  If it exists use "Delete" to delete the file, if it does not exist, do nothing.
4) Use "Open/Create/Replace File" with it set to "create or replace" to create tags.dat in the \data directory.
 
Shouldn't the "Delete" file VI delete a file if it exists, but do nothing if it does not exist?  Instead, if the file doesn't exist, it causes an error.  The same goes for the "New Directory".  It should create the new directory, unless it already exists.   Also is there a way "open/create/replace" with it set to "create or replace" will not prompt the user to ask them if they want to delete the file (if it already exists)?  This is why I have to look for the file first, then delete it if it exists. 
0 Kudos
Message 1 of 4
(4,946 Views)
Are the "C:\programs" and "C:\programs\data" directories fixed, or are they based on the location of the VI/application. Your post seemed to be say both. If they're fixed you obviously don't need to use "Current VI's path".

You might be able to save yourself some coding by using the OpenG function "Create Dir if Non-Existant.vi" and give it the path ""C:\programs\data" - it will create both directories if they're not there. This VI is part of the OpenG File I/O library. You mentioned that you were going to create an .exe, so if you decide to use this please note the distribution requirements for this VI.

Also, I'm not understanding why you're deleting the file if you're specifying the "Open/Create/Replace File" VI do a "create or replace". In general, I tend to stay away from that VI since, frankly, it's a mess. You should be able to accomplish the same thing by using the "New File" function for "C:\programs\data\tags.data" with "overwrite" set to True.
0 Kudos
Message 2 of 4
(4,935 Views)
It is currently in the c:\progams directory, but I want it to be able to run from any directory.  So I do need to obtain the current VI directory. 
 
I will download the OpenG commander.  Thanks. 
 
I am deleting the file becuase the "Open/Create/Replace File" set to "create or replace" will ask if you want to overwrite the file if the file already exists.  I do not want this to happen.  I want it to create the new file if it doesn't exist, and overwrite it (without a prompt) if it does exist.  Maybe "New File" with overwrite set to True will work better.  Thanks. 
0 Kudos
Message 3 of 4
(4,930 Views)
OK, then you just need to set the "advisory dialog" input of the "Open/Create/Replace File" VI to "False".
0 Kudos
Message 4 of 4
(4,927 Views)