Recursive directory search

Share your advanced PureBasic knowledge/code with the community.
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Re: Recursive directory search

Post by firace »

In case it might help some other beginners... Here's AND51's code adapted to PB 5.30+...

Code: Select all


Procedure.l SearchDirectory(dir$, List yourLinkedList.s(), pattern$="", recursive=1)
   Static level.l=-1
   If Not Right(dir$, 1) = "\"
      dir$+"\"
   EndIf
   Protected dir.l=ExamineDirectory(#PB_Any, dir$, pattern$)
   If dir
      While NextDirectoryEntry(dir)
         If DirectoryEntryName(dir) <> "." And DirectoryEntryName(dir) <> ".."
            AddElement(yourLinkedList())
            For n=CountString(dir$, "\")-level To CountString(dir$, "\")
               yourLinkedList()+StringField(dir$, n, "\")+"\"
            Next
            yourLinkedList()+DirectoryEntryName(dir)
            If DirectoryEntryType(dir) = #PB_DirectoryEntry_Directory
               yourLinkedList()+"\"
            EndIf
         EndIf
      Wend
      FinishDirectory(dir)
   EndIf
   Protected all.l=ExamineDirectory(#PB_Any, dir$, "")
   If all
      While NextDirectoryEntry(all)
         If DirectoryEntryType(all) = #PB_DirectoryEntry_Directory And DirectoryEntryName(all) <> "." And DirectoryEntryName(all) <> ".."
            level+1
            SearchDirectory(dir$+DirectoryEntryName(all)+"\", yourLinkedList(), pattern$, recursive)
            level-1
         EndIf
      Wend
      FinishDirectory(all)
   EndIf
   ProcedureReturn ListSize(yourLinkedList())
EndProcedure ; 34 lines by AND51


NewList FilesAndFolders.s()
found = SearchDirectory("C:\WINDOWS", FilesAndFolders(), "sys*", 1)
ForEach FilesAndFolders()
   Debug FilesAndFolders()
Next
Debug found
oO0XX0Oo
User
User
Posts: 78
Joined: Thu Aug 10, 2017 7:35 am

Re: Recursive directory search

Post by oO0XX0Oo »

@AND51

I know it's an old thread but is there any variant of yours that would
allow us to turn on / off the recursion?
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Recursive directory search

Post by mk-soft »

Its a part from my project. Updated to PB v5.x and added parameter recursive=#true

OS=All

Code: Select all

;-TOP

; ***************************************************************************************

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  #PathSeparator = "\"
CompilerElse
  #PathSeparator = "/"
CompilerEndIf
Structure udtFileInfo
  file.s
  size.q
  date.i
EndStructure

Global Stop

; ***************************************************************************************

Procedure InStr(String.s, StringGruppe.s, Separator.s) ; Result BOOL

  Protected gruppe.s, find.s
  
  ; Suchstring vorhanden
  If Not Bool(String)
    ProcedureReturn #False
  EndIf
  ; Gruppenstring vorhanden
  If Not Bool(StringGruppe)
    ProcedureReturn #False
  EndIf
  ; Gruppenstring mit Separator erweitern
  gruppe = StringGruppe
  If Left(StringGruppe, 1) <> Separator
    gruppe = Separator + gruppe
  EndIf
  If Right(StringGruppe, 1) <> Separator
    gruppe + Separator
  EndIf
  ; Suchstring mit Separator erweitern
  find = Separator + String + Separator
  ; Suchen
  If FindString(gruppe, find, 1, #PB_String_NoCase)
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
  
EndProcedure

; ***************************************************************************************

Procedure GetFileInfoRecursive(List FileInfo.udtFileInfo(), Directory.s, Recursive = #True, Exclude.s = "")  
  
  Protected ID, name.s, extension.s
  
  ID = ExamineDirectory(#PB_Any, Directory, "*.*")
  If ID
    While NextDirectoryEntry(ID)  
      name = DirectoryEntryName(ID)  
      If DirectoryEntryType(ID) = #PB_DirectoryEntry_Directory
        If Recursive And name <> ".." And name <> "."
          GetFileInfoRecursive(FileInfo(), Directory+name+#PathSeparator, Recursive, exclude)
        EndIf
      Else
        extension = GetExtensionPart(name)
        If Not InStr(extension, exclude, ";")
          If AddElement(FileInfo()) = 0
            ; Out Of Memory
            Break
          EndIf
          With FileInfo()
            \file = Directory + name
            \size = DirectoryEntrySize(ID)
            \date = DirectoryEntryDate(ID, #PB_Date_Modified)
          EndWith
        EndIf
      EndIf
      ; Abbruch
      If Stop
        Break
      EndIf 
    Wend  
    FinishDirectory(ID)
  EndIf  
  
  ProcedureReturn ListSize(FileInfo())
  
EndProcedure 

; ***************************************************************************************

Global NewList FileInfo.udtFileInfo()

OpenWindow(#PB_Any, 0, 0, 0, 0, "DoEvents", #PB_Window_Invisible | #PB_Window_NoGadgets)

Define dir.s = PathRequester("Path", "")
While WindowEvent() : Wend
Define count = GetFileInfoRecursive(FileInfo(), dir, #True, "bak;tmp")
Debug count
ShowVariableViewer() 
CallDebugger
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
oO0XX0Oo
User
User
Posts: 78
Joined: Thu Aug 10, 2017 7:35 am

Re: Recursive directory search

Post by oO0XX0Oo »

Thank you, mk-soft!

It's working absolutely fine here :mrgreen:
Post Reply