recursively walk a directory

Mac OSX specific forum
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

recursively walk a directory

Post by spacebuddy »

Is there a command in PB that will return all the files and directories? :D


Thanks
User avatar
idle
Always Here
Always Here
Posts: 6191
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: recursively walk a directory

Post 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 

Windows 11, Manjaro, Raspberry Pi OS
Image
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: recursively walk a directory

Post by spacebuddy »

Thanks idle, I tried the code and it works except I get duplicates of every file. :shock:
User avatar
idle
Always Here
Always Here
Posts: 6191
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: recursively walk a directory

Post by idle »

I can't see why it's duplicating on osx?
Can someone test and fix it for OSX please
Windows 11, Manjaro, Raspberry Pi OS
Image
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: recursively walk a directory

Post by spacebuddy »

Tested it on Windows and it works perfectly.

Under Mac OS/X there is something wrong, each file is duplicated.
jamirokwai
Addict
Addict
Posts: 802
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: recursively walk a directory

Post by jamirokwai »

Hi,

works on Mountain Lion with PB 4.61 and PB5b6.

Thanks for sharing!
Regards,
JamiroKwai
User avatar
idle
Always Here
Always Here
Posts: 6191
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: recursively walk a directory

Post by idle »

thanks :D

maybe you could help spacebuddy figure out why it's not working on his system
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply