How to resolve the algorithm Walk a directory/Recursively step by step in the PureBasic programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Walk a directory/Recursively step by step in the PureBasic programming language
Table of Contents
Problem Statement
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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Walk a directory/Recursively step by step in the PureBasic programming language
Source code in the purebasic programming language
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
;- Implementation; Find all .log-files in the C:\Windows tree
ExamineDirectory(1,"C:\WINDOWS\","")
WalkRecursive(1,"C:\WINDOWS\","\.log$")
FinishDirectory(1)
You may also check:How to resolve the algorithm String matching step by step in the Forth programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the BASIC programming language
You may also check:How to resolve the algorithm Reflection/List methods step by step in the PHP programming language
You may also check:How to resolve the algorithm Matrix chain multiplication step by step in the Julia programming language