Walk_Dir RecursivelyYes or RecursivelyNo

Share your advanced PureBasic knowledge/code with the community.
User avatar
useful
Enthusiast
Enthusiast
Posts: 369
Joined: Fri Jul 19, 2013 7:36 am

Walk_Dir RecursivelyYes or RecursivelyNo

Post by useful »

Inspired by http://rapideuphoria.com/lib_u_z.htm#walk_dir

the call is passed as a function parameter for each directory file

Code: Select all

EnableExplicit

#RecursivelyYes = 1
#RecursivelyNo  = 0

Procedure FR(Directory_File_FullName.s) ;File revision
  Debug Directory_File_FullName
EndProcedure

Procedure Walk_Dir(SearchPath.s,*ppp,Recursively)
  
  Protected Directory_ID.i
  Protected Directory_Entry_ID.i
  Protected Directory_Entry_Type.i
  Protected Directory_Entry_Name.s
  Protected Directory_File_FullName.s
  Protected SPath.s
  Protected RightChar.s
  
  RightChar = Right(SearchPath,1)
  SPath     = SearchPath
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    If (RightChar <> "\") 
      SPath = SPath + "\"
    EndIf  
  CompilerElse
    If (RightChar <> "/") 
      SPath = SPath + "/"
    EndIf  
  CompilerEndIf
  
  Directory_ID = ExamineDirectory(#PB_Any, SPath, "*.*")
  
  If (Directory_ID)
    
    Directory_Entry_ID = NextDirectoryEntry(Directory_ID)
    
    While (Directory_Entry_ID)
      
      Directory_Entry_Type = DirectoryEntryType(Directory_ID)
      Directory_Entry_Name = DirectoryEntryName(Directory_ID)
      
      If (Directory_Entry_Type = #PB_DirectoryEntry_File) 
        
        Directory_File_FullName = SPath + Directory_Entry_Name
        
        CallFunctionFast(*ppp,@Directory_File_FullName)
        
      Else 
        If Recursively 
          If ((Directory_Entry_Name <> ".") And (Directory_Entry_Name <> ".."))
            
            Walk_Dir(SPath + Directory_Entry_Name,*ppp,Recursively)
            
          EndIf
        EndIf
      EndIf
      
      Directory_Entry_ID = NextDirectoryEntry(Directory_ID)
      
    Wend
    
  EndIf
  
EndProcedure 

Walk_Dir(GetTemporaryDirectory(),@FR(),#RecursivelyNo) ;#RecursivelyYes

Dawn will come inevitably.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Walk_Dir RecursivelyYes or RecursivelyNo

Post by Sicro »

Good idea.

I have adapted my include "FileSystem/ListDirectoryEntries" in the CodeArchive (look at my signature), so it also uses a callback.
I also mention you in the code header, you've inspired me to use a callback.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Walk_Dir RecursivelyYes or RecursivelyNo

Post by davido »

@useful,
Great.
Thank you for sharing. :D

@Sicro,
Thank you for archiving.
DE AA EB
Post Reply