recursively walk a directory
Posted: Tue Oct 23, 2012 11:32 pm
Is there a command in PB that will return all the files and directories?
Thanks
Thanks
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
EnableExplicit
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
#Cdir = "\"
CompilerElse
#Cdir = "/"
CompilerEndIf
Procedure GetFileList(StartDir.s,List Lfiles.s(),pattern.s="*.*",Recursive=1)
Protected mDir,Directory.s,Filename.s,FullFileName.s, tdir.s,ct,a,depth
Static NewList Lpattern.s()
Static bset,FileCount
If Not bset
pattern = RemoveString(pattern,"*")
ct = CountString(pattern,"|") + 1
ClearList(lpattern())
For a = 1 To ct
AddElement(Lpattern())
Lpattern() = UCase(StringField(pattern,a,"|"))
Next
bset=1
ElseIf depth = 0
bset = 0
EndIf
mDir = ExamineDirectory(#PB_Any, StartDir, "*.*")
If mDir
While NextDirectoryEntry(mDir)
If DirectoryEntryType(mDir) = #PB_DirectoryEntry_File
Directory = startdir
FileName.s = DirectoryEntryName(mDir)
ForEach Lpattern()
If FindString(UCase(FileName),lpattern(), 1)
FullFileName.s = StartDir + #Cdir + FileName
AddElement(LFiles())
Lfiles() = FullFileName
FileCount+1
EndIf
Next
Else
tdir = DirectoryEntryName(mDir)
If tdir <> "." And tdir <> ".."
If Recursive = 1
depth + 1
GetFileList(startDir + #Cdir + tdir,LFiles(),Pattern,Recursive)
EndIf
EndIf
EndIf
Wend
FinishDirectory(mDir)
EndIf
ProcedureReturn FileCount
EndProcedure
Global NewList myfiles.s()
If GetFileList("/home/idle",myfiles(),"*.*",0) ;get all files in the directory not recursive
ForEach myfiles()
Debug myfiles()
Next
EndIf
ClearList(myfiles())
Debug "+++++++++++++++++++++++++++++++++"
If GetFileList("/home/idle",myfiles(),"*.pb|*.txt",1) ;get all pb and txt files in the directory recursively
ForEach myfiles()
Debug myfiles()
Next
EndIf