Alaska Software Inc. - Directory tree
Username: Password:
AuthorTopic: Directory tree
Bengt OveliusDirectory tree
on Thu, 19 Apr 2007 17:20:49 +0200
Is there a good recursive formula that can
collect all files into an array  in a
specified directory tree
..probably based on DIRECTORY()

Thanks and Best regards
Bengt Ovelius
AUGE_OHRRe: Directory tree
on Thu, 19 Apr 2007 18:04:12 +0200
hi,

> Is there a good recursive formula that can
> collect all files into an array  in a
> specified directory tree
> ..probably based on DIRECTORY()

try this :
*** snip ***
#xtranslate .filename      => \[1\]
#xtranslate .filesize      => \[2\]
#xtranslate .filedate      => \[3\]
#xtranslate .filetime      => \[4\]
#xtranslate .fileattrib    => \[5\]

#xtranslate aDIZ           => Stack\[SP,1]
#xtranslate nLEN           => Stack\[SP,2]
#xtranslate nCount         => Stack\[SP,3]
#xtranslate nStartAt       => Stack\[SP,4]

STATIC aAllFiles := {}

PROCEDURE MAIN
...
       searching recursive from cPathName for *.MP3
      RECUDIRS( cPathName, "*.MP3", {|x,y| DoNextWork(x,y, "*.MP3")} )
...
RETURN

PROCEDURE RECUDIRS(p_path, p_wildcard, bblock)

   aDIZ := Directory(p_path+"*.*","D")
   nLEN := LEN(aDIZ)

    make all FileNames LOWER()
   AEVAL(aDIZ, {|x| x.filename := LOWER(x.filename) })
   ASORT(aDIZ,,, {|x,y| x.filename < y.filename})

    First, put all sub-directories
    at the beginning of the list
   ASORT(aDIZ,,, {|x,y| "D" $ x.fileattrib })

    Now, lets find out where the other
    files are
   AEVAL(aDIZ, {|x,i| nStartAt := ;
               IF("D" $ x.fileattrib, i, nStartAt) })

    Sort sub-directories only
   ASORT(aDIZ, 1, nStartAt, {|x,y| x.filename < y.filename})
   nStartAt++

    Sort only the non-directory files.
    Sort Array based on the first subarray
    element
   nLEN := LEN(aDIZ)

*   IF nLEN > 1
*      ASORT( aDIZ, nStartAt,, {|x,y| CompareFile(x.filename,y.filename) } )
*       ASORT( aDIZ, nStartAt,, {|x,y| x.filename < y.filename})
*   ENDIF

   FOR nCount = 1 to nLEN
      IF "D" $ aDIZ[nCount,F_ATTR] .AND. .NOT. ;
              (aDIZ[nCount,F_NAME] = "." .OR.  ;
               aDIZ[nCount,F_NAME] ="..")

         RECUDIRS(p_path+aDIZ[nCount,F_NAME]+"\",p_wildcard,bblock)
      ELSE
         EVAL(bblock,p_path,aDIZ[nCount,F_NAME])
      ENDIF
   NEXT
ENDIF
RETURN

FUNCTION DoNextWork(ccPath,ccFile,ccWildcard)
LOCAL nPosi  := RAT(".",ccWildcard)
LOCAL cExt := SUBSTR(ccWildcard,nPosi,LEN(ccWildcard)-nPosi)

     we only take "some" files with valid Extension (*.MP3)
    IF (cExt) $ ccWildcard
       AADD( aAllFiles, ccPath+ccFile )
    ENDIF
RETURN
*** eof ***
greetings by OHR
Jimmy