Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I know a workaround, where one calculates sizes of all files and then you subtract this from your default space, but is there any direct way to do it? Simplicity and speed are important.

There's the way to calculate free RAM with external call

ReadList["!typeperf \"\\Memory\\Available Bytes\" -sc 1", Word,
RecordLists -> True, WordSeparators -> {","}] // 
ToExpression@Part[#, 2, 2] &

Is there any way to do the same but for disk space?

SOLVED

StringJoin[
ReadList["!dir c:\ ", Word, RecordLists -> True, 
WordSeparators -> {" ", ","}][[-1, 3 ;; -3]]] // ToExpression

gives the amount of free memory on disk C in bytes)

share|improve this question
    
Yes... you can use the same approach, noting that dir gives this information in its output. –  Oleksandr R. Dec 22 '12 at 1:26
1  
There's no built-in way in Mathematica for this so you will need to call an external command or system API function. .NETLink might be useful if you're on Windows (check e.g. this). I'm not on Windows now so I can't test a solution. –  Szabolcs Dec 22 '12 at 1:31
1  
something along the lines of Select[ ReadList["!df", Word, RecordLists -> True, WordSeparators -> {",", " "}], First@# == "/dev/disk1" & ] should do the trick (on OSX and I guess linux) –  acl Dec 22 '12 at 1:40
1  
The Windows performance counter that gives this information is \LogicalDisk(*)\Free Megabytes, so you can use typeperf to get the information if you like. However, this only works if your user account has the "profile system performance" privilege assigned. By default this is only granted to Administrators! –  Oleksandr R. Dec 22 '12 at 1:44
1  
@acl For your df call, I'd display all disks: TableForm[Transpose[{##2}], TableHeadings -> {#, Automatic}] & @@ ReadList["!df -l -k", Word, RecordLists -> True, WordSeparators -> {",", " "}] –  Jens Dec 22 '12 at 5:45
show 1 more comment

3 Answers 3

up vote 6 down vote accepted

We can use JLink for this:

Needs["JLink`"]
InstallJava[];

JavaBlock[JavaNew["java.io.File", "C:\\"]@getFreeSpace[]]
(* 159352233984 *)

We can also find all file system roots and report the total, free and usable disk space:

LoadJavaClass["java.io.File"];

JavaBlock[
  {#@getPath[], #@getTotalSpace[], #@getFreeSpace[], #@getUsableSpace[]}& /@
    java`io`File`listRoots[]
] // TableForm[#, TableHeadings->{None, {"Root","Total", "Free", "Usable"}}]&

table showing disk space

The zeroes here are dismounted drives on my system. The usable space is a concept that varies between operating systems and may take account of disk quotas, permissions, etc.

share|improve this answer
add comment

The package JVMTools (www.jvmtools.net) has the functions FreeDiskSpace[] and TotalDiskSpace[] in its utility functions section, among many other functions. www.jvmtools.net/utility.html

Works on all systems.

share|improve this answer
add comment

Here is a version that passes Directory[] to df so you know how much space there is in Mathematica's current working directory.

I have tried it on Linux but I see no reason it shouldn't work on OSX and other unixy systems that have df.

Options[freeSpace] = {"Path" :> Directory[], "Property" -> 4};
freeSpace[OptionsPattern[]] := 
 Module[{out = 
    ReadList["!df '" <> ToString[OptionValue["Path"]] <> "'"
     ,Word 
     ,RecordLists -> True]
    ,i ,val},
  val = out[[-1, OptionValue["Property"]]];
  Quiet[Check[
    FromDigits[val],
    val, FromDigits::nlst
    ], FromDigits::nlst]
  ]

You can do similarly with @WReach's solution, which would be better since it will work on all systems.

JavaBlock[JavaNew["java.io.File", Directory[]]@getFreeSpace[]]

You can also check how much free space is on another disk, say you store your stuff elsewhere:

freeSpace[]                 (* 8898584 *)
freeSpace["Path"-> "/tmp"]  (* 20958736 *)

You can also extract other information that df gives:

freeSpace["Path" -> "/tmp", "Property" -> 1] (* "/dev/sda5" *)

The different properties are:

    1            2     3     4    5     6
Filesystem      Size  Used Avail Use% Mounted on
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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