Thanks
recursively walk a directory
-
spacebuddy
- Enthusiast

- Posts: 364
- Joined: Thu Jul 02, 2009 5:42 am
recursively walk a directory
Is there a command in PB that will return all the files and directories?
Thanks
Thanks
Re: recursively walk a directory
try this works on linux and windows
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
Windows 11, Manjaro, Raspberry Pi OS


-
spacebuddy
- Enthusiast

- Posts: 364
- Joined: Thu Jul 02, 2009 5:42 am
Re: recursively walk a directory
Thanks idle, I tried the code and it works except I get duplicates of every file. 
Re: recursively walk a directory
I can't see why it's duplicating on osx?
Can someone test and fix it for OSX please
Can someone test and fix it for OSX please
Windows 11, Manjaro, Raspberry Pi OS


-
spacebuddy
- Enthusiast

- Posts: 364
- Joined: Thu Jul 02, 2009 5:42 am
Re: recursively walk a directory
Tested it on Windows and it works perfectly.
Under Mac OS/X there is something wrong, each file is duplicated.
Under Mac OS/X there is something wrong, each file is duplicated.
-
jamirokwai
- Addict

- Posts: 802
- Joined: Tue May 20, 2008 2:12 am
- Location: Cologne, Germany
- Contact:
Re: recursively walk a directory
Hi,
works on Mountain Lion with PB 4.61 and PB5b6.
Thanks for sharing!
works on Mountain Lion with PB 4.61 and PB5b6.
Thanks for sharing!
Regards,
JamiroKwai
JamiroKwai
Re: recursively walk a directory
thanks
maybe you could help spacebuddy figure out why it's not working on his system
maybe you could help spacebuddy figure out why it's not working on his system
Windows 11, Manjaro, Raspberry Pi OS


