This is a procedure to get the size of a folder and all sub-folders, it is non-recursive (in other words it does not need to call itself to handle sub folders).
;Public Domain
Procedure.q GetFolderSize(folder$)
Protected size.q,NewList folders.s(),directory.i,name$,delim$
If FileSize(folder$)=-2
CompilerIf #PB_Compiler_OS=#PB_OS_Windows
delim$="\"
CompilerElse ;MacOS or Linux
delim$="/"
CompilerEndIf
If Right(folder$,1)<>delim$
folder$=folder$+delim$ ;make sure the folder name ends with \ (WIndows) or / (Linux/Mac Os)
EndIf
AddElement(folders())
folders()=folder$ ;add the main folder to the list
While ListSize(folders())
folder$=folders()
DeleteElement(folders(),1) ;remove the folder from the list
directory=ExamineDirectory(#PB_Any,folder$,"")
If directory
While NextDirectoryEntry(directory)
If DirectoryEntryType(directory)=#PB_DirectoryEntry_File
size+DirectoryEntrySize(directory) ;add the entry size to total size so far
Else
name$=DirectoryEntryName(directory)
If (name$<>".") And (name$<>"..") ;. and .. are parent and current folder, we must ignore them to avoid a potential deadlock loop
AddElement(folders())
folders()=folder$+name$+delim$ ;add the folder to the list
EndIf
EndIf
Wend
FinishDirectory(directory)
EndIf
Wend
EndIf
ProcedureReturn size
EndProcedure
path.s = PathRequester("Choose a folder...", "") ;choose a folder ...
Debug GetFolderSize(path)
While this example just gets the size of files / directory size or folder size and the size of sub-folders or sub-directories you can easily edit this to suit other needs.
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
#Delim$ = "\"
CompilerElse ; Mac Os und Linux
#Delim$ = "/"
CompilerEndIf
Procedure.q GetFolderSize(folder$)
Protected size.q,NewList folders.s(),directory.i,name$
If Right(folder$,1)<>#Delim$
folder$=folder$+#Delim$ ;make sure the folder name ends with \ (WIndows) or / (Linux/Mac Os)
EndIf
AddElement(folders())
folders()=folder$ ;add the main folder to the list
While ListSize(folders())
folder$=folders()
DeleteElement(folders(),1) ;remove the folder from the list
directory=ExamineDirectory(#PB_Any,folder$,"")
If directory
While NextDirectoryEntry(directory)
If DirectoryEntryType(directory)=#PB_DirectoryEntry_File
size+DirectoryEntrySize(directory) ;add the entry size to total size so far
Else
name$=DirectoryEntryName(directory)
If (name$<>".") And (name$<>"..") ;. and .. are parent and current folder, we must ignore them to avoid a potential deadlock loop
AddElement(folders())
folders()=folder$+name$+#Delim$ ;add the folder to the list
EndIf
EndIf
Wend
FinishDirectory(directory)
EndIf
Wend
ProcedureReturn size
EndProcedure
path.s = PathRequester("Choose a folder...", "") , choose a folder ...
Debug GetFolderSize(path)
Hope the code is ok
michel51
Mac OS X Snow Leopard (10.6.8 ) Intel
PureBasic V 5.21(x64), V 5.22beta
Updated the first post. Thanks michel51. And if you are wondering why I did the delim a little differently, it is to make sure the procedure is fully self contained.
And to answer the space vs space "issue".
Windows/The OS do restrict access to certain files. Some folders on C are for the OS or machine admin only.
As to the disk size used, that is not possible using PureBasic commands. You need to use the OS API for that as a file may physically be split across multiple storage devices all with different block sizes, a part of a file or a select few files may even be stored in a cloud service.