filename check [message #22084] |
Tue, 24 October 2000 00:00  |
Stuart Colley
Messages: 17 Registered: February 2000
|
Junior Member |
|
|
Can anyone suggest a way to check that a file exists before attempting to
read from it, since the error messages obtained from trying to read from a
non-existing file, don't make it clear that file doesn't exist.
cheers,
S
|
|
|
|
|
Re: filename check [message #22221 is a reply to message #22084] |
Tue, 24 October 2000 00:00   |
Paul van Delst
Messages: 364 Registered: March 1997
|
Senior Member |
|
|
Craig Markwardt wrote:
>
> Stuart Colley <src@star.ucl.ac.uk> writes:
>> Can anyone suggest a way to check that a file exists before attempting to
>> read from it, since the error messages obtained from trying to read from a
>> non-existing file, don't make it clear that file doesn't exist.
>
> Use the ERROR keyword to OPENR to detect an error condition quietly.
> The value returned in this keyword will be non-zero if the file could
> not be opened. Also the !ERROR_STATE system variable will contain
> helpful information describing the error:
>
> NAME STRING 'IDL_M_CNTOPNFIL'
> MSG STRING 'OPENR: Error opening file: snorg.'
> SYS_MSG STRING 'No such file or directory'
>
> According to RSI, the error conditions in !ERROR_STATE.NAME are
> guaranteed to remain constant in future versions of IDL. To implement
> it in practice, consider this:
>
> get_lun, unit
> openr, unit, filename, error=err
>
> if err NE 0 then begin
> free_lun, unit
> message, 'ERROR: could not open '+filename
> endif
> ; Read file ...
>
> I tend to use GET_LUN explicitly so that the unit is guaranteed to be
> valid when it is freed. I've nit-picked about this on the newsgroup
> before.
Seeing the word nit-pick makes me want to so here goes..... :o)
I used to do the above but now I use CATCH for even open errors, forgoing the ERROR=
keyword in the open statements:
CATCH, error_status
IF ( error_status NE 0 ) THEN BEGIN
MESSAGE, !ERR_STRING, /CONTINUE
FREE_LUN, lun ; If needed
CATCH, /CANCEL
RETURN, failure_code
ENDIF
GET_LUN, lun ; Craig is right. Do this!
OPENR, lun, filename
....
..
.
And if one of my nested routines returns an error, then
IF ( result NE success_code ) THEN MESSAGE, 'This routine failed!', /NONAME, /NOPRINT
which sets all the error variables and the CATCH takes over.
For some bizarre reason, despite the double use of MESSAGE in the second example, error
handling this way makes me feel all warm and fuzzy on the inside coz I only have two exit
points - the bad one and the good one. Yay.
O.k. so it's not really a nit-pick, but what the hell. :o)
paulv
--
Paul van Delst Ph: (301) 763-8000 x7274
CIMSS @ NOAA/NCEP Fax: (301) 763-8545
Rm.202, 5200 Auth Rd. Email: pvandelst@ncep.noaa.gov
Camp Springs MD 20746
|
|
|
Re: filename check [message #22222 is a reply to message #22084] |
Tue, 24 October 2000 00:00   |
Dick Jackson
Messages: 347 Registered: August 1998
|
Senior Member |
|
|
"Stuart Colley" <src@star.ucl.ac.uk> wrote...
>
> Can anyone suggest a way to check that a file exists before attempting to
> read from it, since the error messages obtained from trying to read from a
> non-existing file, don't make it clear that file doesn't exist.
Of course, the solutions in all the other replies point to why this was
needed, and now appears in IDL 5.4:
IDL> Print, File_Test('image.gif')
0
IDL> Print, File_Test('image.tif')
1
File_Test has many fancy keywords for "what kind of file do you expect to
find?", etc.:
[, /DIRECTORY | , /EXECUTABLE | , /READ | , /REGULAR | , /WRITE | ,
/ZERO_LENGTH] [, GET_MODE=variable]
Cheers,
--
-Dick
Dick Jackson / dick@d-jackson.com
D-Jackson Software Consulting / http://www.d-jackson.com
Calgary, Alberta, Canada / Voice/Fax: +1-403-242-7398
|
|
|
Re: filename [message #23644 is a reply to message #22084] |
Wed, 07 February 2001 09:20  |
Pavel A. Romashkin
Messages: 531 Registered: November 2000
|
Senior Member |
|
|
I face this all the time, and must confess in being too lazy to write a
solution. I usually search for a delimiter from the string end
(Strpos(path, delimiter, /reverse)) or use Get_Path keyword, Strlen the
path and Strmid the returned value from that location to the end:
full_loc = Dialog_pickfile(get_path=path)
length = strlen(path)
file_name = strmid(full_loc, length)
Three lines of code; oh well. Need a wrapper function to make it one
line :-)
Cheers,
Pavel
Gernot Reishofer wrote:
>
> hi,
>
> I have to save two files with the same filename but different
> extensions. Using DIALOG_PICKFILE creates path+filename+extention.
> Can you experts tell me the shortest way to get the filename avoiding
> too many string operations?
>
> thanks for help
>
> Gernot
|
|
|
Re: filename [message #23651 is a reply to message #22084] |
Wed, 07 February 2001 05:21  |
Martin Schultz
Messages: 515 Registered: August 1997
|
Senior Member |
|
|
Gernot Reishofer wrote:
>
> hi,
>
> I have to save two files with the same filename but different
> extensions. Using DIALOG_PICKFILE creates path+filename+extention.
> Can you experts tell me the shortest way to get the filename avoiding
> too many string operations?
>
> thanks for help
>
> Gernot
I'm not sure it is the shortest, but it is sure convenient to use a
utility routine from one of several public domain librariers for this.
In my own library this is extract_filename.pro. You can browse through
this and other libraries at
http://www.mpimet.mpg.de/~schultz.martin/idl/
CHeers,
Martin
--
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
[[ Dr. Martin Schultz Max-Planck-Institut fuer Meteorologie [[
[[ Bundesstr. 55, 20146 Hamburg [[
[[ phone: +49 40 41173-308 [[
[[ fax: +49 40 41173-298 [[
[[ martin.schultz@dkrz.de [[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
|
|
|