Task

Walk a given directory tree and print files matching a given pattern.


Note: This task is for recursive methods.   These tasks should read an entire directory tree, not a single directory.


Note: Please be careful when running any code examples found here.



Related task

   Walk a directory/Non-recursively   (read a single directory). 


Procedure.s WalkRecursive(dir,path.s,Pattern.s="\.txt$")
  Static RegularExpression 
  If Not RegularExpression
    RegularExpression=CreateRegularExpression(#PB_Any,Pattern)
  EndIf
 
  While NextDirectoryEntry(dir)
    If DirectoryEntryType(dir)=#PB_DirectoryEntry_Directory
      If DirectoryEntryName(dir)<>"." And DirectoryEntryName(dir)<>".."
        If ExamineDirectory(dir+1,path+DirectoryEntryName(dir),"")
          WalkRecursive(dir+1,path+DirectoryEntryName(dir)+"\",Pattern)
          FinishDirectory(dir+1)
        Else
          Debug "Error in "+path+DirectoryEntryName(dir)
        EndIf
      EndIf
    Else ; e.g. #PB_DirectoryEntry_File 
      If MatchRegularExpression(RegularExpression,DirectoryEntryName(dir))
        Debug DirectoryEntryName(dir)
      EndIf
    EndIf
  Wend
EndProcedure