source: trunk/SRC/Utilities/find.pro @ 285

Last change on this file since 285 was 285, checked in by smasson, 17 years ago

allways return back a scalar is /firstfound used

  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1;+
2;
3; @file_comments
4; based on <proidl>FILE_SEARCH</proidl>, but it is possible to specify
5; a set of possibles names and a different set of
6; possibles directories names.
7; By default look for files included in !path
8;
9; all <proidl>FILE_SEARCH</proidl> keywords can be used.
10;
11; @categories
12; find a file
13;
14; @param FILEIN {in}{required} {type=scalar string or array of strings}
15; File name[s] to match. Input names specifications may contain
16; wildcard characters, enabling them to match multiple files
17; (see <proidl>FILE_SEARCH</proidl> for more informations). By default and if
18; necessary, <pro>find</pro> is looking for filename and also for filename
19; completed with '.pro'
20;
21; @keyword FIRSTFOUND
22; activate this keyword to stop looking for the file as soon as we
23; found one. Return a scalar string containing the first file found
24;
25; @keyword IODIRECTORY {type=scalar string or array of strings} {default=['.',!path]}
26; Directories names where we are looking for the file.
27; Different directories can be separated by
28; path_sep(/search_path) (':' on unix type machine) as it is done
29; to define !path.
30; Note that if filename's dirname is different from '.', this
31; keyword is not taken into account.
32;
33; @keyword LOOKALLDIR
34; activate to look for the file (with a recursive search if needed)
35; in . iodir, homedir, !path + the DATA:TestsData directory if it exists.
36;
37; @keyword NOPRO
38; activate to avoid the automatic search of filename completed with '.pro'
39;
40; @keyword ONLYPRO
41; force to look only at file ending with .pro
42;
43; @keyword ONLYNC
44; force to look only at file ending with .nc
45;
46; @keyword RECURSIVE
47; performs recursive searching of directory hierarchies.
48; In a recursive search, <pro>find</pro> looks recursively for any and all
49; subdirectories in the file hierarchy rooted at the IODIRECTORY argument.
50;
51; @keyword REPERTOIRE
52; obsolete. keep for compatibility, use IODIRECTORY keyword
53;
54; @keyword UNIQUE
55; activate to make sure that each element of the output vector is unique.
56;
57; @keyword TRYFIND
58; if the file was not found and this keyword is activated, <pro>find</pro>
59; will call
60; itself with the keywords /LOOKALLDIR and /FIRSTFOUND to try to find
61; the file we are looking for. Note that if the file was found at the
62; first try this keyword as no effect (which is not the case with LOOKALLDIR)
63;
64; @keyword _EXTRA
65; Used to pass keywords
66;
67; @returns
68; A scalar or array variable of string type, containing the
69; name (with the full path of the matching files. If no files
70; exist with names matching the input arguments, <pro>find</pro> returns
71; the scalar string : 'NOT FOUND'
72;
73; @examples
74; IDL> print, find('*loadct')
75;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
76;   /usr/local/rsi/idl_6.0/lib/loadct.pro
77; IDL> print, find('*loadct', iodir=!dir,/recursive)
78;   /usr/local/rsi/idl_6.0/lib/loadct.pro
79;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
80; IDL> print, find('*loadct.pro')
81;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
82;   /usr/local/rsi/idl_6.0/lib/loadct.pro
83; IDL> print, find('*loadct',/nopro)
84;   NOT FOUND
85; IDL> print, find('*loadct', iodir = '/usr/local/rsi/idl_6.0/lib')
86;   /usr/local/rsi/idl_6.0/lib/loadct.pro
87; IDL> print, find('*loadct', iodir = '/usr/local/rsi/idl_6.0/lib', /test_write)
88;   NOT FOUND
89; IDL> print, find('*loadct', iodir = '/usr/local/rsi/idl_6.0/lib', /recursive)
90;   /usr/local/rsi/idl_6.0/lib/loadct.pro
91;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
92; IDL> print, find('mesh*', iodirectory = [iodir, !path])
93;   /Users/sebastie/DATA/ORCA2/meshmaskORCA2closea.nc
94;   /Users/sebastie/IDL/meshmaskclosesea.pro
95;   /Users/sebastie/IDL/meshmaskclosesea.pro~
96;   /Users/sebastie/SAXO_RD/Obsolete/meshlec.pro
97;   /usr/local/rsi/idl_6.0/lib/mesh_obj.pro
98;
99; @history
100; Sebastien Masson (smasson\@lodyc.jussieu.fr)
101;                       28/4/1999
102;                       6/7/1999: compatibility mac and windows
103; June 2005: Sebastien Masson: cleaning, use for file_* functions
104;
105; @version
106; $Id$
107;
108;-
109;
110FUNCTION find, filein, IODIRECTORY = iodirectory, RECURSIVE = recursive $
111               , REPERTOIRE = repertoire, NOPRO = nopro, ONLYPRO = onlypro $
112               , ONLYNC = onlync, UNIQUE = unique, FIRSTFOUND = firstfound $
113               , LOOKALLDIR = LOOKALLDIR, TRYFIND = tryfind, _EXTRA = ex
114;
115  compile_opt idl2, strictarrsubs
116;
117; define where we look for the file
118  cd, current = current
119  current = current + path_sep()
120  CASE 1 OF
121    keyword_set(lookalldir):BEGIN
122@cm_general
123      dirnames = [current, iodir, homedir, !path]
124      tstdtadir= file_dirname(find('find', /onlypro), /mark_directory)
125      parent = path_sep(/parent_directory)+path_sep()
126      tstdtadir = (file_search(tstdtadir+parent+parent+'DATA/TestsData'))[0]
127      IF tstdtadir NE '' THEN dirnames = [tstdtadir, dirnames]
128    END
129    keyword_set(iodirectory): dirnames = iodirectory
130    keyword_set(repertoire): dirnames = repertoire
131    ELSE: dirnames = [current, !path]
132  ENDCASE
133  tmp = dirnames
134  dirnames = 'dummy'
135  FOR i = 0, n_elements(tmp)-1 DO $
136    dirnames = [dirnames, strsplit(tmp[i], path_sep(/search_path), /extract)]
137  dirnames = dirnames[1:*]
138;
139  fileout = 'dummy'
140  FOR i = 0, n_elements(filein)-1 DO BEGIN
141    dir = file_dirname(filein[i])
142    base = file_basename(filein[i])
143; try to complete the file name with .pro or .nc if needed...
144    CASE 1 OF
145      keyword_set(onlypro):BEGIN
146        promiss = strpos(base, '.pro', /reverse_search)
147        promiss = promiss - (strlen(base) - 4)
148        bad = where(promiss NE 0 OR strlen(base) LE 4, cnt)
149        IF cnt NE 0 THEN base[bad] = base[bad] + '.pro'
150      end
151      keyword_set(onlync):BEGIN
152        ncmiss = strpos(base, '.nc', /reverse_search)
153        ncmiss = ncmiss - (strlen(base) - 3)
154        bad = where(ncmiss NE 0 OR strlen(base) LE 3, cnt)
155        IF cnt NE 0 THEN base[bad] = base[bad] + '.nc'
156      END
157      ELSE:if strmid(base, 0, 1, /reverse_offset) NE '*' $
158        AND NOT keyword_set(nopro) THEN base = base + '{.pro,}'
159    ENDCASE
160; use dirnames only if dir eq '.'
161    IF dir EQ  '.' THEN BEGIN
162      if keyword_set(recursive) THEN $
163        found = file_search(dirnames, base, _extra = ex) $
164        ELSE found = file_search(dirnames + '/' + base, _extra = ex)
165    ENDIF ELSE found = file_search(dir + '/' + base, _extra = ex)
166    IF found[0] NE '' THEN BEGIN
167      IF keyword_set(firstfound) THEN return, found[0]
168      fileout = [fileout, found]
169    ENDIF
170  ENDFOR
171
172  IF n_elements(fileout) EQ 1 THEN fileout = 'NOT FOUND' $
173  ELSE fileout = fileout[1:*]
174;
175  IF n_elements(fileout) GT 1 THEN BEGIN
176    IF keyword_set(unique) THEN fileout = fileout[uniq(fileout, sort(fileout))]
177  ENDIF ELSE fileout = fileout[0]
178;
179  IF keyword_set(lookalldir) AND fileout[0] EQ 'NOT FOUND' $
180     AND NOT keyword_set(recursive) THEN $
181        filout = find(file_basename(filein[0]), /lookalldir $
182                      , /recursive, _extra = ex)
183;
184  IF keyword_set(tryfind) AND fileout[0] EQ 'NOT FOUND' THEN BEGIN
185    fileout = find(file_basename(filein[0]), /lookalldir, /firstfound, _extra = ex)
186    fileout = fileout[0]
187  ENDIF
188;
189  RETURN, fileout
190END
Note: See TracBrowser for help on using the repository browser.