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

Re: find_subtree.pro

Kristian Kjaer (kristian.kjaer@risoe.dk) writes: 

> 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.) 

Here is a little thing I coded up today when I should have
been doing something a whole lot more useful. I still don't
fully understand recursive functions, so these kinds of things
always suck me in. :-(

Anyway, this appears to work under the extensive testing
I've subjected it to. :^)

It is specific for Windows machines. The names that it
returns are given relative to the target directory name
(to which I apply NO error checking!). It will, perhaps, 
give you some ideas.

As always, I'm open for gentle criticism, which I will
probably deserve for this one. :-)



Function AllDir, target

; This function returns the names of all the directories
; rooted at the "target" directory. The function is specific
; for the Windows operating system. The return names are
; given with respect to the target name.

    ; Default directory is current directory.

   CD, Current=target
   target = target + '\'

    ; Switch to target directory.

CD, target, Current=thisDirectory

    ; Find the files in the target directory.

theseFiles = Findfile('*', Count=count)
IF count EQ 0 THEN RETURN, ''

    ; Find the directories in the file list. Directories
    ; end with a "\" character.

endCharPos = StrLen(theseFiles) - 1
FOR j=0,count-1 DO BEGIN
   IF theseFiles[j] NE '.\' AND theseFiles[j] NE '..\' THEN BEGIN
      lastChar = StrMid(theseFiles[j], endCharPos[j], 1)
      IF lastChar EQ '\' THEN BEGIN
         IF N_Elements(theseDirs) EQ 0 THEN $
            theseDirs = [theseFiles[j], AllDir(theseFiles[j])] ELSE $
            theseDirs = [theseDirs, theseFiles[j], AllDir(theseFiles[j])]
   ENDIF ELSE theseDirs = ''

   ; Add the target name.

theseDirs = target + theseDirs

   ; Go back to the starting directory.

CD, thisDirectory

    ; Remove null strings and non-unique values.

returnValue = theseDirs[Where(theseDirs NE target) > 0]
returnValue = returnValue[Uniq(returnValue)]

    ; Return the list.
RETURN, returnValue

David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438
Coyote's Guide to IDL Programming: http://www.dfanning.com/