[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: find_subtree.pro



Kristian Kjaer wrote:

> Hi,
>
> I am looking for piece of code - similar to findfile() - which will
> return the fully-qualified paths to all subdirectories of the default
> directory, or return the fully-qualified paths to, say, all files *.dat
> in all subdirectories of the default directory.
>
> (I use IDL 5 on WinNT.)
>
> Any hints appreciated!
>
> -------------------- Msg. from: ---------------------------
> Kristian Kjaer             'phone +45 4677 4709 (dir. line)
> Physics Department,        Fax    +45 4677 4790
> Risoe National Laboratory, 'phone +45 4677 4677 (sw.-board)
> DK-4000 Roskilde,
> Denmark                   e-mail: Kristian.Kjaer@Risoe.DK

Dear Kristian

here is some piece of code, which you can extend yourself.
You need in addition stress and strep from the Ray Sterner lib.


;
; Copyright (c) 1997, Forschungszentrum Juelich GmbH ICG-1
; All rights reserved.
; Unauthorized reproduction prohibited.
;
;       This software may be used, copied, or redistributed as long as it
is not
;       sold and this copyright notice is reproduced on each copy made.
This
;       routine is provided as is without any express or implied
warranties
;       whatsoever.
;+
; NAME:
;       get_dir
;
; PURPOSE:
;   This function finds all subdirectories of a given path
;
; CATEGORY:
;   DATAFILES/FILE
;
; CALLING SEQUENCE:
;   Result=get_dir(path)
;
; INPUTS:
;       path: The start path where get_dir should look for subdirectories
;
;
; KEYWORD PARAMETERS:
;       all:  in addition to the normal subdirectories the entries . and
..
;       sort: sorts the output
;       help: gaves a short description
;
; OUTPUTS:
;       The result of this function is a string array of subdirectories
;
;
; EXAMPLE:
;   Result=get_dir('/usr/local')
;   or
;   Result=get_dir('\windows')
;
; MODIFICATION HISTORY:
;       Written by:     R.Bauer (ICG-1), May 1997
;-

function get_dir,help=help,inpath,sort=sort,all=all
;debug,'1.1 RB  1997-Dec-22'

   if keyword_set(help) then begin
      print,"print,get_dir('/')"
      print,'========================================================='
      return,''
   endif

   if n_elements(inpath) gt 0 then begin
      cd,current=oldpath        ; alten path saven
      cd,inpath
   endif

   if strupcase(!version.os) eq 'WIN32' or strupcase(!version.os) eq 'WIN'
then delim = '*.*' ELSE  delim='-Fd *'
   alle_ein=findfile(delim)

   if n_elements(inpath) gt 0 then cd,oldpath ; alten path restoren
   if strupcase(!version.os) eq 'AIX' THEN delim = '/' ELSE delim = '\'
   if strupcase(!version.os) eq 'AIX' then alle_ein =
['./','../',alle_ein]

   subs=where(strpos(alle_ein,delim) ge 0,count)

   if count gt 0 then begin
      nur_subdirs=alle_ein(subs)

      n_Anz=n_elements(nur_subdirs)-1

      for i=0,n_anz do begin
         nur_subdirs(i)=stress(nur_subdirs(i),'D',0,delim)
      endfor

      if keyword_set(sort) then $
       nur_subdirs=nur_subdirs(sort(nur_subdirs))

      if keyword_set(all) then return, nur_subdirs

      if n_anz ge  2 then nur_subdirs=nur_subdirs(2:n_anz) else
nur_subdirs=''

      return,nur_subdirs
   endif


   if count eq -1 then return,''


END


===== CUT HERE ======


FUNCTION files_in_dirs ,dir=dir,pattern=pattern

IF n_elements(dir) EQ 0 THEN dir = '.'
IF n_elements(pattern) EQ 0 THEN pattern = '*.*'
dirs = get_dir(dir)

n_dirs = n_elements(dirs)
FOR i=0,n_dirs-1 DO BEGIN
files  = findfile(dir+dirs(i)+'/'+pattern)
IF n_elements(result) EQ 0 THEN result = files ELSE result =
[result,files]
ENDFOR

return,result


END

==== CUT HERE ======

PRO ex_subtree

print,files_in_dirs(dir='../',pattern='*.pro')

END







--
R.Bauer

Institut fuer Stratosphaerische Chemie (ICG-1)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de