7
$\begingroup$

I use Mathematica10 in my Win7 and I want to estimate the amount of disk space occupied by each dirs.

I try the code(the code was copied from How do I find the amount of free space on disk?, actually I don't know how it works at all):

dirsofD = FileNames["*", "D:\\"]

and

sizeoddirsofD = 
ToExpression@
StringJoin[
 ReadList["!dir " <> #, Word, RecordLists -> True, 
   WordSeparators -> {" ", ","}][[-2, 3 ;; -2]]] & /@ dirsofD

However, the code above only count the files directive under certain dir while the files under subdirs of given dir are missed, which means the search depth is only one. How could I get the whole size of all files of certain dir?

$\endgroup$
1
  • $\begingroup$ If you are okay with using another program, WinDirStat has got to be one of the coolest little freeware programs to do this. $\endgroup$
    – Jason B.
    Commented Dec 3, 2015 at 9:27

2 Answers 2

10
$\begingroup$

There is also a function in the Internal` context (for now) that does that

Internal`DirectoryByteCount[$InstallationDirectory]

(* 6794576686 *)

which is in agreement with what my OS reports for that directory

enter image description here

$\endgroup$
0
4
$\begingroup$

An estimate can be obtained without resorting to the underlying OS.

FileByteCount can be used to obtain the size of a file and FileNames can list files to any depth so combining these in to a function:

recursiveFolderSize[folder_String] := 
 Plus @@ Quiet[
   FileByteCount /@ FileNames["*", folder, Infinity] /. $Failed -> 0, 
   FileByteCount::fdir]

FileByteCount gives a message and returns $Failed for directories so the function handles these cases.

Also note that directories themselves take up disk space so the total returned will be an underestimate.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.