B4J Question size of a directory in bytes, KB or MB in B4J

strupp01

Active Member
Licensed User
Longtime User
Is it possible to determine the size of a directory in bytes, KB or MB in B4J?
 

EnriqueGonzalez

Expert
Licensed User
Longtime User
without testing it.


B4X:
Sub SizeOfFilesInFolder(f As String) As Long
    Dim total As Long
    For Each filename As String In File.ListFiles(f)
        If File.IsDirectory(f, filename) = False Then
            total = total + File.Size(f, filename)
        Else
            total = total + SizeOfFilesInFolder(f&"\"&filename)
        End If
    Next
    Return total
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The reason that I didn't implement the recursive solution is that there are a few pitfalls. Whether they are relevant or not depends on your app requirements:
1. It can be slow if you choose a folder such as the root folder.
2. On Linux there might be symbolic links that can cause the recursive function to never end (until the stack is overflowed).
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Another option is to let the OS calculate it for you.
Depending on the OS you could shell to commands like "dir <myfolder> /a /s" or "du -sh <folder.." or similar, then parse the result.

udg
 
Last edited:
Upvote 0
Top