Determine available disk space
Show older comments
I was acquiring a bunch of images and got a write error from imwrite that it couldn't write the file. It turned out the problem was that the drive was full. I know how many images I need to save and I would like to determine, in advance, if there is enough space to capture all the images that I need.
Is there a MATLAB function to determine the amount of available drive space? A bonus would be the amount of disk space used so far, and the total capacity of the drive.
I tried
[status, cmdout] = system('dir c:')
0 File(s) 0 bytes
11 Dir(s) 46,905,581,568 bytes free
but then it involves some additional tricky parsing of cmdout string, plus it takes a few seconds. I was wondering if there was a more direct way to get the available disk space as a number. Like a built-in function or something. Searches of the documentation and Answers forum turned up nothing.
Answers (3)
Image Analyst
on 23 Jan 2023
1 vote
I didn't find any MATLAB functions but a java method works:
>> free = java.io.File("D:").getFreeSpace()
free =
4.6906e+10
1 Comment
dpb
on 23 Jan 2023
Much easier/simpler than shelling out, indeed, IA!
I "know nuthink!" to speak of of Java so don't ever know where/how to go look; I didn't find a convenient wrapper to the Win API already written so reverted to PS...
dpb
on 23 Jan 2023
Try this for comparison if on Windows...
!powershell "Get-PSDrive C | Select-Object @{Name='drive';Expression={$_.Name}}, @{Name='used';Expression={$_.Used}}, @{Name='free';Expression={$_.Free}} | ConvertTo-Csv -NoTypeInformation"
on local drive (can't run on unix server) returns
>> !powershell "Get-PSDrive C | Select-Object @{Name='drive';Expression={$_.Name}}, @{Name='used';Expression={$_.Used}}, @{Name='free';Expression={$_.Free}} | ConvertTo-Csv -NoTypeInformation"
"drive","used","free"
"C","144044228608","111368957952"
>>
4 Comments
dpb
on 23 Jan 2023
OBTW, if you replace the single drive letter spec with
PS C:\Users\Duane> Get-PSDrive -PSProvider FileSystem |
Select-Object @{Name='drive';Expression={$_.Name}},
@{Name='used';Expression={$_.Used}},
@{Name='free';Expression={$_.Free}} |
ConvertTo-Csv -NoTypeInformation
"drive","used","free"
"C","144038768640","111374417920"
"D","11353010176","3392061440"
"E","1574912","0"
"F","169615802368","815735255040"
"H","2893224779776","954929598464"
"M","2893224779776","954929598464"
"S","2893224779776","954929598464"
"W","2893224779776","954929598464"
PS C:\Users\Duane>
will return for all accessible drives. The last four above are mapped drives to different locations on a remote system drive hence the values are all the same for the drive. That portion over the network introduces a noticeable delay...
Image Analyst
on 23 Jan 2023
How do you get the numbers into a variable?
>> s = !powershell "Get-PSDrive C | Select-Object @{Name='drive';Expression={$_.Name}}, @{Name='used';Expression={$_.Used}}, @{Name='free';Expression={$_.Free}} | ConvertTo-Csv -NoTypeInformation"
s = !powershell "Get-PSDrive C | Select-Object @{Name='drive';Expression={$_.Name}}, @{Name='used';Expression={$_.Used}}, @{Name='free';Expression={$_.Free}} | ConvertTo-Csv -NoTypeInformation"
↑
Invalid use of operator.
>> s = powershell "Get-PSDrive C | Select-Object @{Name='drive';Expression={$_.Name}}, @{Name='used';Expression={$_.Used}}, @{Name='free';Expression={$_.Free}} | ConvertTo-Csv -NoTypeInformation"
s = powershell "Get-PSDrive C | Select-Object @{Name='drive';Expression={$_.Name}}, @{Name='used';Expression={$_.Used}}, @{Name='free';Expression={$_.Free}} | ConvertTo-Csv -NoTypeInformation"
↑
Invalid use of operator.
Oh, picky, picky.... :)
You have to manage to wrap all the above into the form to be used with system() in order to capture the return value. I was too lazy to go to the trouble earlier until/unless OP thought it was enough quicker to make worthy of the effort.
Use
>> [stat,res]=system('powershell -command "Get-PSDrive C | Select-Object @{Name=''drive'';Expression={$_.Name}}, @{Name=''used'';Expression={$_.Used}}, @{Name=''free'';Expression={$_.Free}} | ConvertTo-Csv -NoTypeInformation"')
stat =
0
res =
'"drive","used","free"
"C","144046850048","111366336512"
'
>>
which has to dispatch powershell and pass it the command string to execute. system is brain-enfeebled if not entirely brain-dead in that it can't deal with anything except a char() string so to wrap the overall command in double-quotes that it appears PS needs, one then must find and double-tick all the single quotes around the parameters in the command. Doable, but painfully tedious...
PS is capable, but is terribly arcane and documentation is almost impossible to find...whoever invented this was a masochist, for sure...btw, the shortname/alias for "Get-PSDrive" is "gdr" and PS is case-insensitive; "GDR", "gdr", "GDr", "gDr", ... are all the same to it.
dpb
on 23 Jan 2023
PS. I've yet to figure out that there's a way to substitute PS for CMD with system -- TMW apparently doesn't believe there are any other shells. Same problem with Win10 using the JPSoft CMD replacement shell -- I forget where it broke; with earlier releases when back under NT4 and subsequent, MATLAB used to honor the command interpreter set with the system startup; I've been unable to make that work with recent releases although I've not made any attempts for quite_a_long_time_now™, just having become resigned to it although it is a pain to not have all the extra features available via system sometimes.
Walter Roberson
on 23 Jan 2023
Edited: Image Analyst
on 23 Jan 2023
0 votes
4 Comments
Image Analyst
on 23 Jan 2023
Edited: Image Analyst
on 23 Jan 2023
I tried that but can't figure it out. I know you have a MAC so maybe you can't help further but this does not work:
NET.addAssembly('System.IO')
obj = System.IO.AvailableFreeSpace;
Unable to resolve the name 'System.IO.AvailableFreeSpace'.
Error in test7 (line 2)
obj = System.IO.AvailableFreeSpace;
This might be the way to go though. Did I hear that they were going to strip Java out of MATLAB so my Answer java.io.File("D:").getFreeSpace() may not work after a certain version?
dpb
on 23 Jan 2023
I think you'll first have to create an instance of the DriveInfo class and then may have to iterate through the collection based on the sample code in the above link. I didn't poke at it much, but a quick perusal didn't show me how to get the specific drive although there probably is a method.
Looks superficially a lot like writing COM for Excel object model -- can become extremely tedious to get from the top level object down to the parts/object/property of interest.
Walter Roberson
on 23 Jan 2023
I would expect to start with something like
obj = System.IO.DriveInfo;
and that would probably be some kind of array.
But I am not running Windows so it is not easy for me to test.
Image Analyst
on 23 Jan 2023
I tried that first. It also gave the "Unable to resolve the name" error.
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!