Re: Directory recursion
Posted: Tue Feb 21, 2023 11:09 pm
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
Structure DirStruct
Directory.s
List FileList.s()
SubDirCount.l
EndStructure
Global Dir.DirStruct
Procedure.i RecurseThruDirectories(*directoryStruct.DirStruct)
Protected NewList TempList.s(),exa,temp$,dir$
AddElement(TempList())
TempList()=*directoryStruct\Directory
If Right(TempList(), 1) <> #PS$
TempList() = TempList() + #PS$
EndIf
*directoryStruct\SubDirCount=0
ClearList(*directoryStruct\FileList())
While ListSize(TempList())
FirstElement(TempList())
dir$=TempList()
exa=ExamineDirectory(#PB_Any,dir$,"*.*")
If exa
While NextDirectoryEntry(exa)
temp$=DirectoryEntryName(exa)
If DirectoryEntryType(exa)=#PB_DirectoryEntry_Directory
If ReplaceString(temp$,".","")<>""
*directoryStruct\SubDirCount+1
AddElement(TempList())
TempList()=dir$+temp$+#PS$
EndIf
Else
AddElement(*directoryStruct\FileList())
*directoryStruct\FileList()=dir$+temp$
Debug dir$+temp$
EndIf
Wend
EndIf
FirstElement(TempList())
DeleteElement(TempList())
Wend
EndProcedure
OpenConsole()
Print("Directory: ")
dir$=Input()
If dir$<>""
Dir\Directory=dir$
RecurseThruDirectories(Dir)
PrintN("Subdirectories: "+Str(Dir\SubDirCount))
PrintN("Files: "+Str(ListSize(Dir\FileList())))
PrintN("FileList:")
ForEach Dir\FileList()
PrintN(Dir\FileList())
Next
EndIf
PrintN("Done")
Then let's see if we can teach you something, shall we? Simple integer addition is atomic. That means the addition is guaranteed to succeed, regardless if the process is threaded or not. What isn't guaranteed, is to know the value of the integer, immediately prior to the addition. This is where we would use atomic functions. Had I intended to present threads, I would have done so. What I did I present, was recursion.jacdelad wrote: Tue Feb 21, 2023 9:41 pmBut again, and without trying to be counterproductive, I think this way (including the global variables) is not a good way to do the task.
Code: Select all
directoryNumber = ExamineDirectory(#PB_Any, directoryName, "*.*")
If directoryNumber
while NextDirectoryEntry(directoryNumber)
;...
wend
Endif
Code: Select all
Procedure.q Fibonacci_Rec(n.i)
If (n < 2)
ProcedureReturn n
Else
ProcedureReturn Fibonacci_Rec(n - 1) + Fibonacci_Rec(n - 2)
EndIf
EndProcedure
Procedure.q Fibonacci_Iter(n.i)
Protected vnow.q = 0
Protected vnext.q = 1
Protected vtemp.q
While n-1 > 0
n-1
vtemp = vnow + vnext
vnow = vnext
vnext = vtemp
Wend
ProcedureReturn vnext;
EndProcedure
For a = 0 To 20
Debug Str(a) + " = " + Str(Fibonacci_Rec(a))
Debug Str(a) + " = " + Str(Fibonacci_Iter(a))
Next
Code: Select all
ReplaceString(temp$,".","")<>""Code: Select all
If name = "." Or name = ".."
Continue
EndIfCode: Select all
Define name.s = "..."
*c.Character = @name
If *c\c = '.'
*c + SizeOf(Character)
If *c\c = '.'
*c + SizeOf(Character)
If *c\c = 0
Debug ".."
End
; Continue
EndIf
ElseIf *c\c = 0
Debug "."
End
; Continue
EndIf
EndIf
Debug "yes: " + nameCode: Select all
EnableExplicit
DisableDebugger
Define i, StartTime
Define name.s = "."
Define name.s = "dfgdsfgdsfgdsf.dfg"
Define Res.s, Res1.s, Res2.s
Define *c.Character
StartTime = ElapsedMilliseconds()
For i = 1 To 1000000
*c.Character = @name
If *c\c = '.'
*c + SizeOf(Character)
If *c\c = '.'
*c + SizeOf(Character)
If *c\c = 0
Continue
EndIf
ElseIf *c\c = 0
Continue
EndIf
EndIf
Next
Res.s = Str(ElapsedMilliseconds() - StartTime) + " ms"
StartTime = ElapsedMilliseconds()
For i = 1 To 1000000
If name = "." Or name = ".."
Continue
EndIf
Next
Res1.s = Str(ElapsedMilliseconds() - StartTime) + " ms"
StartTime = ElapsedMilliseconds()
For i = 1 To 1000000
If ReplaceString(name,".","")=""
Continue
EndIf
Next
Res2.s = Str(ElapsedMilliseconds() - StartTime) + " ms"
EnableDebugger
Debug Res
Debug Res1
Debug Res2
Code: Select all
i = 0
id = ExamineDirectory(#PB_Any, "C:\Windows\", "")
If id
While NextDirectoryEntry(id)
Debug DirectoryEntryName(id)
i+1
If i = 3
End
EndIf
Wend
FinishDirectory(id)
EndIf@mk-soft,mk-soft wrote: Wed Feb 22, 2023 6:34 pm ...
Directory entry "..." is the reference to the parent directory and must be ignored.
...