필터 지우기
필터 지우기

Determine available disk space

조회 수: 48 (최근 30일)
Mark Hayworth
Mark Hayworth 2023년 1월 23일
편집: dpb 2023년 1월 23일
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.

답변 (3개)

Image Analyst
Image Analyst 2023년 1월 23일
I didn't find any MATLAB functions but a java method works:
>> free = java.io.File("D:").getFreeSpace()
free =
4.6906e+10
  댓글 수: 1
dpb
dpb 2023년 1월 23일
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
dpb 2023년 1월 23일
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
dpb
dpb 2023년 1월 23일
편집: dpb 2023년 1월 23일
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 2023년 1월 23일
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
Walter Roberson 2023년 1월 23일
편집: Image Analyst 2023년 1월 23일
  댓글 수: 4
Walter Roberson
Walter Roberson 2023년 1월 23일
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 2023년 1월 23일
I tried that first. It also gave the "Unable to resolve the name" error.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by