Page 1 of 1

recursively walk a directory

Posted: Tue Oct 23, 2012 11:32 pm
by spacebuddy
Is there a command in PB that will return all the files and directories? :D


Thanks

Re: recursively walk a directory

Posted: Wed Oct 24, 2012 2:04 am
by idle
try this works on linux and windows

Code: Select all


EnableExplicit 

CompilerIf #PB_Compiler_OS = #PB_OS_Windows 
   #Cdir = "\" 
CompilerElse 
   #Cdir = "/"
CompilerEndIf 

Procedure GetFileList(StartDir.s,List Lfiles.s(),pattern.s="*.*",Recursive=1)
   Protected mDir,Directory.s,Filename.s,FullFileName.s, tdir.s,ct,a,depth
   
   Static NewList Lpattern.s()
   Static bset,FileCount 
      
   If Not bset
      pattern = RemoveString(pattern,"*")
      ct = CountString(pattern,"|") + 1
      ClearList(lpattern())
      For a = 1 To ct 
           AddElement(Lpattern())
           Lpattern() = UCase(StringField(pattern,a,"|"))
       Next
       bset=1
    ElseIf depth = 0 
       bset = 0
    EndIf 
 
  mDir = ExamineDirectory(#PB_Any, StartDir, "*.*") 
  If mDir 
    While NextDirectoryEntry(mDir)
      If DirectoryEntryType(mDir) = #PB_DirectoryEntry_File
          Directory = startdir
          FileName.s = DirectoryEntryName(mDir)
           ForEach Lpattern()
                If FindString(UCase(FileName),lpattern(), 1)
                  FullFileName.s = StartDir + #Cdir + FileName
                  AddElement(LFiles()) 
                  Lfiles() = FullFileName
                  FileCount+1
               EndIf
            Next  
           
      Else
         tdir = DirectoryEntryName(mDir)
         If tdir <> "." And tdir <> ".."
            If Recursive = 1
               depth + 1
            GetFileList(startDir + #Cdir + tdir,LFiles(),Pattern,Recursive)
           EndIf
         EndIf
      EndIf
   Wend
   FinishDirectory(mDir)
  EndIf
   
  ProcedureReturn FileCount

EndProcedure


Global NewList myfiles.s() 

If GetFileList("/home/idle",myfiles(),"*.*",0)  ;get all files in the directory not recursive 
  ForEach myfiles() 
     Debug myfiles() 
  Next   
EndIf 

ClearList(myfiles())
Debug "+++++++++++++++++++++++++++++++++"

If GetFileList("/home/idle",myfiles(),"*.pb|*.txt",1)  ;get all pb and txt files in the directory recursively  
  ForEach myfiles() 
     Debug myfiles() 
  Next   
EndIf 


Re: recursively walk a directory

Posted: Wed Oct 24, 2012 7:11 pm
by spacebuddy
Thanks idle, I tried the code and it works except I get duplicates of every file. :shock:

Re: recursively walk a directory

Posted: Wed Oct 24, 2012 7:56 pm
by idle
I can't see why it's duplicating on osx?
Can someone test and fix it for OSX please

Re: recursively walk a directory

Posted: Wed Oct 24, 2012 10:26 pm
by spacebuddy
Tested it on Windows and it works perfectly.

Under Mac OS/X there is something wrong, each file is duplicated.

Re: recursively walk a directory

Posted: Thu Oct 25, 2012 6:42 am
by jamirokwai
Hi,

works on Mountain Lion with PB 4.61 and PB5b6.

Thanks for sharing!

Re: recursively walk a directory

Posted: Thu Oct 25, 2012 7:19 am
by idle
thanks :D

maybe you could help spacebuddy figure out why it's not working on his system