MAC
Can I get the size (memory) of a folder and/or drive?
For example, I want to know that a selected folder (subfolders and files) takes up 356MB of memory.
Thanks
MAC
Can I get the size (memory) of a folder and/or drive?
For example, I want to know that a selected folder (subfolders and files) takes up 356MB of memory.
Thanks
Check out the “du” shell command. “du -sh /path/to/folder” is a good start.
Not sure how that works?
I use FolderItem to select the folder ( folderitem.nativePath)
I was hoping there was a way through the folderitem that I could get the size…
There’s nothing built in to do that. You would have to recursively iterate through its children and total up their sizes. You would be better starting a shell and using the du command Eric provided.
The folderitem per se is just a directory entry, but I assume you are looking for the size including all files and subfolders and all their children? To get that you need to recurse over all the folder contents, or use something that does that process for you. The “du” shell command is one free way to do that relatively easily without your own loop (and is likely far more efficient than a loop you code yourself).
If you have the MBS plugins, see their DirectorySizeMBS class.
Eric/Ian/Douglas
Thanks for your input and suggestions. And getting back to me so fast.
Enjoy the holidays!
Try this:
Public Function GetFolderSizeInKB(f as FolderItem) As integer
' returns the total size of the folder tree in KB, using the shell 'du' command
' returns 0 if folder doesn't exist
' returns -1 upon error
' supports macOS and Linux only
#if TargetWin32
raise new PlatformNotSupportedException
#endif
if f = nil or not f.exists then
return 0
end if
dim cmd as string = "du -sk " + f.ShellPath
dim sh as new Shell
sh.ExecuteMode = Shell.ExecuteModes.Synchronous ' Synchronous: wait for the shell to finish
sh.Execute(cmd)
dim result as string = sh.ReadAll
' result will look like this upon success
' 16536880 /path/to/folderItem
' upon failure
' du: /path/to/folderItem: Operation not permitted
' get the first word, using ASCII 9 (the TAB character) as the separator
dim sizeS as string = result.NthField(chr(9),1)
' if we see 'du' it's probably an error
if sizeS.IndexOf("du") > 0 then
return -1
end if
' convert the string to integer and return the number
return val(sizeS)
End Function
@Mike_D du returns size in blocks by default. You could use “du -sk” to get the results in kilobytes.
Mike/Ian
Much thanks!!! Its getting there… I am using du sk
The size i’m getting back is close:
143GB vs 147GB Actual
13.4 GB vs 13.7 actual
If I can’t get it closer, this should be good enough…
Also, I tried a flash drive… and I received some feedback in the " sizeS as string" about “Operation not permitted”, but the number it also returned was also close 1.1 GB vs 1.2 GB actual
Will continue to play with this…
Thanks again
Jim
Good catch, I’ve updated the code to use the “k” option
k in this case is 1024, rather than 1000 (used by Finder).
This may just be the difference between 1GB defined as 1000MB vs 1024MB. That is,
Decades ago disk manufacturer marketing teams decided that the drives sounded bigger if they defined K as meaning 1000 instead of the 1024 factor you get from an exponent of 2. And they could claim that the K prefix just means Kilo which means 1000 so they could claim it was not false advertising. And it has been confusing to compare numbers ever since (IMHO).
Actually, you can blame the International Electrotechnical Commission (IEC) for switching from 1024 to 1000. According to them 1024bytes should be called Kibibyte. Completely annoying to be honest.
All
Thanks for all your help. This works great!!
Jim
Next question: Can I get the amount of free memory?
For example: Can I find the available free memory on a flash drive or external Hard drive for example?
Thanks
“Available memory” is a better term.
Started looking around and found this…
Shell.Execute "df -h " + f.NativePath
Do you mean free space? If yes, that’s a complicated topic because in Apple’s mind your free space is theirs to fill with garbage. You need NSURLMBS from the MBS plugin for that.