know the size of folder

Just starting out? Need help? Post your questions and find answers here.
ADN
User
User
Posts: 17
Joined: Fri May 16, 2003 9:25 am
Location: France

know the size of folder

Post by ADN »

Hi, :lol:
How to know the size of folder as that FileSize(NomFichier$) ?

sorry for my english

Thank
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

You have to add up the sizes of all files inside the directory.
The easiest way is a recursive procedure call like this one:

Code: Select all

Procedure.l DirectorySize(ID.l, Directory.s)
  Protected Size.l
  If ExamineDirectory(ID, Directory, "*.*")
    Repeat
      Entry.l = NextDirectoryEntry()
      If Entry = 1
        Size + DirectoryEntrySize()
      ElseIf Entry = 2
        Name.s = DirectoryEntryName()
        If Name <> ".." And Name <> "."
          Size + DirectorySize(ID+1, Directory+Name+"\")
          UseDirectory(ID)
        EndIf
      EndIf
    Until Entry = 0
  EndIf
  ProcedureReturn Size
EndProcedure

; Use 0 as ID, it is only needed for the recursive calls
Debug DirectorySize(0, "C:\WINNT\")
Debug DirectorySize(0, "C:\Temp\")
Timo
quidquid Latine dictum sit altum videtur
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> The easiest way is a recursive procedure call

But be aware that if you have many big files then the total size may be
reported incorrectly due to limitations in PureBasic... I had to remove such
a procedure from one of my apps, and am waiting for big number support
in PureBasic before putting it back in.
Post Reply