Determine available disk space

57 Ansichten (letzte 30 Tage)
Mark Hayworth
Mark Hayworth am 23 Jan. 2023
Bearbeitet: dpb am 23 Jan. 2023
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.

Antworten (3)

Image Analyst
Image Analyst am 23 Jan. 2023
I didn't find any MATLAB functions but a java method works:
>> free = java.io.File("D:").getFreeSpace()
free =
4.6906e+10
  1 Kommentar
dpb
dpb am 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...

Melden Sie sich an, um zu kommentieren.


dpb
dpb am 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 Kommentare
dpb
dpb am 23 Jan. 2023
Bearbeitet: dpb am 23 Jan. 2023
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
dpb am 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.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 23 Jan. 2023
Bearbeitet: Image Analyst am 23 Jan. 2023
  4 Kommentare
Walter Roberson
Walter Roberson am 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
Image Analyst am 23 Jan. 2023
I tried that first. It also gave the "Unable to resolve the name" error.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by