Level order tree traversal with ExamineDirectory()
Posted: Fri Jun 22, 2018 7:29 am
I don't recall ever doing this before in PureBasic so it was a fun little excersize. I needed to walk a directory tree and while recursion is always simpler to implement, I prefer a stack-based approach.
This example program will output the all directories recursively starting from the directory where the executable is run. If you don't provide an alternative, the default location is your operating system's temp directory.
A summary on how breadth-first (level order) traversal works can be found here:
https://www.cs.bu.edu/teaching/c/tree/breadth-first/
This example program will output the all directories recursively starting from the directory where the executable is run. If you don't provide an alternative, the default location is your operating system's temp directory.
A summary on how breadth-first (level order) traversal works can be found here:
https://www.cs.bu.edu/teaching/c/tree/breadth-first/
Code: Select all
CompilerIf #PB_Compiler_OS=#PB_OS_Windows
#PS$="\"
CompilerElse
#PS$="/"
CompilerEndIf
Structure DirectoryStack
Id.i
PathName.s
EndStructure
ExaminePath.s=RTrim(GetPathPart(ProgramFilename()),#PS$)
NewList Stack.DirectoryStack()
Directory=ExamineDirectory(#PB_Any,ExaminePath.s,"*.*")
If Not Directory
End
EndIf
AddElement(Stack())
Stack()\Id=Directory
Stack()\PathName=ExaminePath.s
While ListSize(Stack())>0
NextPath=Stack()\Id
CurrentPath.s=Stack()\PathName.s
While NextDirectoryEntry(NextPath)
EntryName.s=DirectoryEntryName(NextPath)
If EntryName.s="." Or EntryName.s=".."
Continue
EndIf
If DirectoryEntryType(NextPath)=#PB_DirectoryEntry_Directory
ChangeCurrentElement(Stack(),LastElement(Stack()))
Directory=ExamineDirectory(#PB_Any,CurrentPath.s+#PS$+EntryName.s,"*.*")
If Directory
AddElement(Stack())
Stack()\Id=Directory
Stack()\PathName.s=CurrentPath.s+#PS$+EntryName.s
EndIf
;/ Debug
Debug CurrentPath.s+#PS$+EntryName.s
ChangeCurrentElement(Stack(),FirstElement(Stack()))
EndIf
Wend
FinishDirectory(NextPath)
DeleteElement(Stack())
ChangeCurrentElement(Stack(),FirstElement(Stack()))
Wend