LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Does a File Exist ?

Hello again everyone.

I want to determine if a file name already exists in a directory.
I have the following little vi to test with:

Text Constant Icon Icon
"C:\Data\test.tmp" -> String to Path -> New Directory -> error out

goes to "Unbundle By Name" which splits it into:
boolean - status
int - code
string - source

In the directory C:\Data I have the following:

Array 1
Array 2
decode01
test.tmp 1KB Text Document

{I have tried to make sure the case of all the characters is the
same}

The Help for the "New Directory" function says the
following:

"If a file or directory already exists at the specified location,
function returns an error instead of overwritting the existing
file or directory"

Now when I run this little program the error out is:

status - F
code - 0
source -

Which says there is no error and not even a warning plus in the
directory C:\Data I have a directory called "test.tmp".

Did I miss something it looks like nothing does what its suppose to.
I tried to use size as an indication of a files existents but the size
always came back zero (0) even when the file is reported by Windows to
be 1K in size. Also tried to use the error output of the Open icon,
hoping I would get an error if the file did not exist and no error if
it did. I alway came out T for status with a code of 7 and if the file
was not in the directory it would create one, not what I wanted.

Thanks in advance for your help.

Regards,

Steve Drake

PS: I really should say thank you to all t
he people who have helped
me with previous problems I have learned alot from you, but
I'm still a newbe with problems like the above.
0 Kudos
Message 1 of 4
(3,089 Views)
Steve see comments in you message
Steve Drake wrote:

> Hello again everyone.
>
> I want to determine if a file name already exists in a directory.
> I have the following little vi to test with:
>
> Text Constant Icon Icon
> "C:\Data\test.tmp" -> String to Path -> New Directory -> error out
>

[SNIP some stuff]

>
>
> Which says there is no error and not even a warning plus in the
> directory C:\Data I have a directory called "test.tmp".

The VI you created is functioning properly, unfortunately it is not what you
want.
The VI is creating a new DIRECTORY, not a new FILE.
In the VI described above replace the New Directory with New File, from the
same
pallete and it will do what you want.

>
>
> Did I miss something it looks like not
hing does what its suppose to.
> I tried to use size as an indication of a files existents but the size
> always came back zero (0) even when the file is reported by Windows to
> be 1K in size.

If you were looking at the directory of the same name as the file then it's
size is zero.
The size of an empty folder is reported as zero by windows.

> Also tried to use the error output of the Open icon,
> hoping I would get an error if the file did not exist and no error if
> it did. I alway came out T for status with a code of 7 and if the file
> was not in the directory it would create one, not what I wanted.

Not sure about this, I use this method all the time and it returns 7 if the
file does not exist but it does not create the file.

Hope this helps and let me know if you need more advice.
Kevin Kent
0 Kudos
Message 2 of 4
(3,089 Views)
> I want to determine if a file name already exists in a directory.
> I have the following little vi to test with:
>

I may be off the mark, but there is a list directory function that
returns the contents. Using the string functions can tell you if
there is a match. I also believe that the Open function will do
this, and it is a very common technique. You may want to start
with a clean and know directory and run a test.

Greg McKaskle
0 Kudos
Message 3 of 4
(3,089 Views)
Spot on. Using the List directory VI, match first string VI and testing if the index output of the latter is > 0 works for me! Thanks,

Chris Van Den Elzen
0 Kudos
Message 4 of 4
(3,089 Views)