Changeset 11 for trunk/Utilities/isafile.pro
- Timestamp:
- 04/27/06 11:05:35 (18 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/Utilities/isafile.pro
r9 r11 3 3 ;------------------------------------------------------------ 4 4 ;+ 5 ; NAME: isafile5 ; NAME: isafile 6 6 ; 7 ; PURPOSE:find a file and check if it is a real file! 7 ; PURPOSE: same as find.pro except that as long as the file is 'NOT FOUND', 8 ; isafile calls dialog_pickfile, to ask the user to select a file. 8 9 ; 9 ; CATEGORY: for open10 ; CATEGORY: io 10 11 ; 11 ; CALLING SEQUENCE:filename = isafile( )12 ; CALLING SEQUENCE:filename = isafile([filein]) 12 13 ; 13 ; INPUTS:none 14 ; INPUTS:optional:a proposed name. If neither filein 15 ; input parameter of filename keyword are defined, 16 ; the ask the user to choose a file. 14 17 ; 15 18 ; KEYWORD PARAMETERS: 16 ; FILENAME: a proposed filename.17 ; IODIRECTORY: a directory where must be the file.18 ; /NEW:to specify that filename is a new file19 19 ; 20 ; OUTPUTS:the filename 20 ; FILENAME: a proposed filename. 21 ; 22 ; IODIRECTORY: a directory where we look for the file. this 23 ; keyword is taken into account only if the dirmame 24 ; of filein or filename is '.' 25 ; 26 ; /NEW:to specify that filename is a new file and that 27 ; we should check only its path 28 ; 29 ; RECURSIVE: performs recursive searching of directory hierarchies. 30 ; In a recursive search, find looks recursively for any and all 31 ; subdirectories in the file hierarchy rooted at the IODIRECTORY 32 ; argument. 33 ; 34 ; all find, file_search and dialog_pickfile keywords (like title) 35 ; 36 ; OUTPUTS:the filename with its path 21 37 ; 22 38 ; COMMON BLOCKS:none … … 28 44 ; EXAMPLE: 29 45 ; 46 ; IDL> print, isafile('/Users/sebastie/SAXO_RD/Commons/cm_4mesh.pro') 47 ; /Users/sebastie/SAXO_RD/Commons/cm_4mesh.pro 48 ; IDL> print, isafile('cm_4mesh.pro', iodir = '/Users/sebastie/SAXO_RD/Commons') 49 ; /Users/sebastie/SAXO_RD/Commons/cm_4mesh.pro 50 ; IDL> print, isafile('cm_4mesh.pro', iodir = !path) 51 ; /Users/sebastie/SAXO_RD/Commons/cm_4mesh.pro 52 ; IDL> print, isafile('cm_4mesh.pro', iodir = '/Users/sebastie/SAXO_RD', /recursive) 53 ; /Users/sebastie/SAXO_RD/Commons/cm_4mesh.pro 54 ; IDL> print, isafile('cm_4mesh.pro', iodir = getenv('HOME'), /recursive) 55 ; /Users/sebastie/SAXO_RD/Commons/cm_4mesh.pro 56 ; IDL> print, isafile('fake_file.pro') 57 ; 30 58 ; MODIFICATION HISTORY:Sebastien Masson (smasson@lodyc.jussieu.fr) 31 59 ; 11/2/2000 60 ; June 2005: Sebastien Masson: cleaning, use for file_* functions 32 61 ;- 33 62 ;------------------------------------------------------------ 34 63 ;------------------------------------------------------------ 35 64 ;------------------------------------------------------------ 36 FUNCTION isafile, FILENAME = filename, IODIRECTORY = iodirectory, FILTER = filter, NEW = new 65 FUNCTION isafile, filein, FILENAME = filename, IODIRECTORY = iodirectory $ 66 , NEW = new, RECURSIVE = RECURSIVE, _extra = ex 37 67 ;------------------------------------------------------------ 38 ; we find the filename. 39 ;------------------------------------------------------------ 40 ; is filename given by the FILENAME keyword a good one? 41 ; si iodirectory n''est pas definit on l''initialise au repertoire courant 42 if NOT keyword_set(filename) then filename = 'nimportequoi' 43 thisOS = strupcase(strmid(!version.os_family, 0, 3)) 44 CASE thisOS OF 45 'MAC':sep = ':' 46 'WIN':sep = '\' 47 ELSE:sep = '/' 48 ENDCASE 49 ; we complete filename by iodirectory? 50 if strpos(filename, sep) EQ -1 then begin 51 if NOT keyword_set(iodirectory) then cd, current = iodirectory 52 ; si iodirectory ne finit pas par sep on le rajoute 53 if rstrpos(iodirectory, sep) NE strlen(iodirectory)-1 then $ 54 iodirectory = iodirectory+sep 55 filename = iodirectory+filename 56 ENDIF 57 if keyword_set(new) then return, filename 58 test = findfile(filename) ; le nom cherche correspond bien a un fichier? 59 while test[0] EQ '' OR n_elements(test) GT 1 do BEGIN ; on en cherche un tant qu''il ne correspond a rien! 60 test = test[0] 61 if keyword_set(filter) AND thisOS NE 'MAC' AND thisOS NE 'WIN' then $ 62 filename = dialog_pickfile(path = iodirectory, filter = filter) $ 63 ELSE filename = dialog_pickfile(path = iodirectory) 64 if filename EQ '' then return, report('read_data canceled') 65 test = findfile(filename) 66 endwhile 67 68 return, filename 69 end 68 CASE 1 OF 69 (size(filein, /type))[0] EQ 7:fileout = filein 70 keyword_set(filename):fileout = filename 71 ELSE:fileout = 'file that is not existing' 72 ENDCASE 73 if size(fileout, /type) NE 7 THEN return, -1 74 ; 75 basename = file_basename(fileout) 76 dirname = file_dirname(fileout) 77 ; should we redefine dirname? 78 if keyword_set(iodirectory) AND dirname EQ '.' then dirname = iodirectory 79 ; 80 if keyword_set(new) then return, dirname + path_sep() + basename 81 ; 82 fileout = find(basename, iodirectory = dirname $ 83 , recursive = recursive, /unique, /firstfound, _extra = ex) 84 WHILE fileout[0] EQ 'NOT FOUND' DO BEGIN 85 fileout = dialog_pickfile(path = dirname[0], _extra = ex) 86 if fileout EQ '' THEN RETURN, report('check/find file canceled') 87 ; check again everything... 88 basename = file_basename(fileout) 89 dirname = file_dirname(fileout) 90 ; check if the name of the dirname is ok 91 dirname = isadirectory(dirname, title = 'choose a directory for the file ' $ 92 + basename) 93 ; if we cancel the check 94 IF size(dirname, /type) NE 7 THEN return, report('check/find file canceled') 95 fileout = find(basename, iodirectory = dirname $ 96 , recursive = recursive, /unique, /firstfound, _extra = ex) 97 ENDWHILE 98 ; 99 RETURN, fileout 100 END
Note: See TracChangeset
for help on using the changeset viewer.