[Solved] Directory loop without global?
Posted: Sun Jun 09, 2024 1:52 am
Anyone know how I can convert this code to NOT use a global variable to hold the directory size? Because my app needs to call this routine multiple times now from different threads, and the var is getting incorrectly updated even though my app is thread-safe. 
Source -> https://www.purebasic.fr/english/viewto ... 82#p171382

Source -> https://www.purebasic.fr/english/viewto ... 82#p171382
Code: Select all
Global foldersize.q ; Don't want to rely on this being global.
Procedure.q FolderSizeRoutine(dir$)
dir=ExamineDirectory(#PB_Any,dir$,"")
If dir
While NextDirectoryEntry(dir)
If DirectoryEntryType(dir)=#PB_DirectoryEntry_File
foldersize+DirectoryEntrySize(dir)
Continue
ElseIf Not DirectoryEntryName(dir)="." And Not DirectoryEntryName(dir)=".."
FolderSizeRoutine(dir$+DirectoryEntryName(dir)+"\")
Continue
EndIf
Wend
FinishDirectory(dir)
EndIf
ProcedureReturn foldersize
EndProcedure
Procedure.q FolderSize(dir$)
If Right(dir$,1)<>"\"
dir$+"\"
EndIf
foldersize=0
FolderSizeRoutine(dir$)
ProcedureReturn foldersize
EndProcedure
Debug FolderSize("C:\Windows\System32\") ; Example of current non-thread use.