comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » filename check
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
filename check [message #22084] Tue, 24 October 2000 00:00 Go to next message
Stuart Colley is currently offline  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 #22206 is a reply to message #22084] Wed, 25 October 2000 00:00 Go to previous messageGo to next message
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
Dear Stuart,

there are two more routines avaliable for you from our library.



file_exist:

; PURPOSE:
; The result of this function is 1 if a file exist and 0 if not
;
; CATEGORY:
; DATAFILES
;
; CALLING SEQUENCE:
; Result=file_exist(file_name,[valid=valid])


http://www.fz-juelich.de/icg/icg1/idl_icglib/idl_source/idl_ html/dbase/download/file_exist.tar.gz
http://www.fz-juelich.de/icg/icg1/idl_icglib/idl_source/idl_ html/dbase/download/file_exist.sav



is_read_access:
; PURPOSE:
; Test if read access is possible of a file
;
; CATEGORY:
; PROG_TOOLS
;
; CALLING SEQUENCE:
; result=is_read_access(path_and_file)
;

http://www.fz-juelich.de/icg/icg1/idl_icglib/idl_source/idl_ html/dbase/download/is_read_access.tar.gz
http://www.fz-juelich.de/icg/icg1/idl_icglib/idl_source/idl_ html/dbase/download/is_read_access.sav



For further routines and copyright and licence.
http://www.fz-juelich.de/icg/icg1/idl_icglib/idl_lib_intro.h tml


regards
Reimar
Re: filename check [message #22218 is a reply to message #22084] Tue, 24 October 2000 00:00 Go to previous messageGo to next message
Edward Max Pottasch is currently offline  Edward Max Pottasch
Messages: 3
Registered: October 2000
Junior Member
Stuart Colley <src@star.ucl.ac.uk> wrote in message
news:Pine.OSF.3.96.1001024143333.20360F-100000@zuaxp7.star.u cl.ac.uk...
>
> 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
>


hi Stuart,

my way of IDL programming is rather simple since I am only familiar with the
older versions of IDL (v.3.4.1). Anyway this works for me:

ciao, Ed



fn='filename'

; check if file exists
ff=FINDFILE(fn)

IF (ff(0) EQ fn) THEN BEGIN

; do your stuff

ENDIF
Re: filename check [message #22221 is a reply to message #22084] Tue, 24 October 2000 00:00 Go to previous messageGo to next message
Paul van Delst is currently offline  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 Go to previous messageGo to next message
Dick Jackson is currently offline  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 Go to previous message
Pavel A. Romashkin is currently offline  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 Go to previous message
Martin Schultz is currently offline  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 [[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: ENVI slope function
Next Topic: Re: HELP: How to read 10-bit image file

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:39:35 PDT 2025

Total time taken to generate the page: 0.00646 seconds