cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Checking if a file exists (v13)

JBlackhole
16-Pearl

Checking if a file exists (v13)

Hi

I have got a template doing a lot of calculations. At the end of message is displayed if the calculations are acceptable or not (based on some user's inputs). An "audit" file is created automatically containing key outputs. The file name is based on one of the inputs. The problem I have is that a file with the same name is overwritten automatically. The question is therefore: Can I check if the file already exists and if it is the case add an incremental number to it (filename_02.txt)?

Thanks

Regards

JXB
8 REPLIES 8

You coud try reading it and trapping errors with the on error construct.

� � � � Tom Gutman

On 12/13/2007 3:26:52 AM, jxb wrote:
== The question is therefore: Can I check if the file already exists and if it is the case add an incremental number to it (filename_02.txt)?

Yes. I think I've got a component-based file existence function in one of my libraries - I'm Mathcadless at the moment so it'll be at least 5 or 6 hrs before I check.

Stuart

On 12/13/2007 9:20:19 AM, stuartafbruff wrote:
On 12/13/2007 3:26:52 AM, jxb wrote:
== The question is therefore: Can I check if the file already exists and if it is the case add an incremental number to it (filename_02.txt)?
>> Yes. I think I've got a component-based file existence function in one of my libraries - I'm Mathcadless at the moment so it'll be at least 5 or 6 hrs before I check.

I've checked, the FileExists worksheet exists ...

Stuart

You can even do it in mathcad without scripting
(works in v 11, not below, might work in higher versions)
Luc

On 12/14/2007 4:27:34 AM, Luc_Meekes wrote:
== You can even do it in mathcad without scripting

Yes, READBIN is better than READPRN - I've added it to my original worksheet.

== (works in v 11, not below, might work in higher versions)

Works in M11..M14, Luc

Stuart

>>Yes, READBIN is better than READPRN - I've added it to my original worksheet.<<

Depends a bit on what you are trying to accomplish. Using READPRN checks not only for existance but also for a certain level of validity. For the original purpose, checking to see whether a result file had already been generated, that seems appropriate.

For the READBIN I would suggest adding some parameters to only read one byte. No need to read the entirety of a large file, at eight bytes per character, to determine if it exists. For large files the time and space considerations could be significant.

� � � � Tom Gutman

Improved version
Thanks Tom and Stuart

There's still one quirk with this:
when the file contains 0 bytes it also reports it as non-existent.

Luc

>>when the file contains 0 bytes it also reports it as non-existent.<<

For most applications that should be acceptable -- even desirable. Such a file is not really a file, but an abortive attempt to write a file. Unless one is using the file system for some sort of signaling, by the presence or absence of files in a directory, zero length files are not meaningful. Since the intent of this application is to avoid overwriting a previous result, only a file that is a proper previous result should be preserved.

� � � � Tom Gutman
Top Tags