Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
free disk space on network drive

Subject: free disk space on network drive

From: Albert

Date: 16 Jun, 2010 21:39:04

Message: 1 of 18

I want to be able to find the free disk space for a network drive. Ideally a platform independent solution would be best. At a minimum this needs to be done without admin rights on Windows and on Linux. I have not had any luck.
The best i have done so far is parsing S=evalc('system(''dir'')')
Anything better?
Thanks.
-yot

Subject: free disk space on network drive

From: Walter Roberson

Date: 16 Jun, 2010 21:47:36

Message: 2 of 18

Albert wrote:
> I want to be able to find the free disk space for a network drive.
> Ideally a platform independent solution would be best. At a minimum
> this needs to be done without admin rights on Windows and on Linux. I
> have not had any luck.

There is no platform-independent answer for this, as some systems have
"infinite" disk storage (managed through techniques such as tape migration.)

The amount of space that is free _now_ is almost never the amount of space
that you can write to.

If the question "can I produce a file this big?" then fopen() the file for
"a+", fseek() to the offset-1, fwrite() a byte and check to see if the fwrite
worked; if it did, then fseek() to the beginning of file and go ahead and use
the file. This will, however, not work if your file system knows about "holes"
and if your file system detects this case as signaling that you want hole to
be made.

Subject: free disk space on network drive

From: Albert

Date: 16 Jun, 2010 22:09:03

Message: 3 of 18

Walter Roberson <roberson@hushmail.com> wrote in message <hvbgth$6u9$1@canopus.cc.umanitoba.ca>...
> There is no platform-independent answer for this, as some systems have
> "infinite" disk storage (managed through techniques such as tape migration.)
>
> The amount of space that is free _now_ is almost never the amount of space
> that you can write to.
>
> If the question "can I produce a file this big?" then fopen() the file for
> "a+", fseek() to the offset-1, fwrite() a byte and check to see if the fwrite
> worked; if it did, then fseek() to the beginning of file and go ahead and use
> the file. This will, however, not work if your file system knows about "holes"
> and if your file system detects this case as signaling that you want hole to
> be made.

The questions is more: I want to run a process that will need 10GB of disk space, is there enough free space for the process to complete successfully?

Subject: free disk space on network drive

From: Walter Roberson

Date: 17 Jun, 2010 00:19:40

Message: 4 of 18

Albert wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <hvbgth$6u9$1@canopus.cc.umanitoba.ca>...
>> There is no platform-independent answer for this, as some systems have
>> "infinite" disk storage (managed through techniques such as tape
>> migration.)
>>
>> The amount of space that is free _now_ is almost never the amount of
>> space that you can write to.
>>
>> If the question "can I produce a file this big?" then fopen() the file
>> for "a+", fseek() to the offset-1, fwrite() a byte and check to see if
>> the fwrite worked; if it did, then fseek() to the beginning of file
>> and go ahead and use the file. This will, however, not work if your
>> file system knows about "holes" and if your file system detects this
>> case as signaling that you want hole to be made.
>
> The questions is more: I want to run a process that will need 10GB of
> disk space, is there enough free space for the process to complete
> successfully?

What is the difference between what you are trying to do and my "If the
question is ..." technical discussion ? It is useless to ask _now_ about
whether 10GB total will be available _later_ on a shared filesystem. If
you want to know if you can write a file that size, write a file that
size (fseek + fwrite) and then you will have the space locked up (unless
the underlying filesystem supports "holes".)

Subject: free disk space on network drive

From: Albert

Date: 17 Jun, 2010 22:49:04

Message: 5 of 18

I do not want to make a 10 GB file. The process will make 100s of files added up to a large size (20 some GB actually).

Of course, the original question is "how much free space is on the drive"? So to this end you would have me loop over creating larger files until the drive was full and I could not? Also if the drive only had 10 GB free and I used it, other process may break. I would be best to know how much was free prior to filling it up.

A further issue is that this drive has a backup system where deleted files are moved to the backup prior to the space being free. The delay in freeing space can be over a day. So if I fill the space using your technique I will still not be able to run the process as the drive would then be full even if it was not before I started.

But this trick would work for the simple question on a drive without this type of backup.

Subject: free disk space on network drive

From: Walter Roberson

Date: 18 Jun, 2010 03:21:24

Message: 6 of 18

Albert wrote:
> I do not want to make a 10 GB file. The process will make 100s of files
> added up to a large size (20 some GB actually).
>
> Of course, the original question is "how much free space is on the
> drive"? So to this end you would have me loop over creating larger
> files until the drive was full and I could not? Also if the drive only
> had 10 GB free and I used it, other process may break. I would be best
> to know how much was free prior to filling it up.

Please don't let me interfere with you making your own mistakes.

Subject: free disk space on network drive

From: Nils Tobias

Date: 21 Sep, 2010 10:42:04

Message: 7 of 18

Hello,

I have the same question but I dont think it was solved in this thread.

Of course the solution would work but I also just want to know if there is let's say 10GB free disk space. I guess there must be something like 'df' for linux that simply tells me in a string or integer how much is left. For me it's not important if I can store the exact number of bytes I just need to have a rough idea.

Any suggestions?

Thanks
NTK

Subject: free disk space on network drive

From: Jan Simon

Date: 21 Sep, 2010 20:37:07

Message: 8 of 18

Dear Nils,

perhaps this helps and does not depend on the platform:
  FileObj = java.io.File('D:\');
  FileObj.getFreeSpace
  FileObj.getTotalSpace
  FileObj.getUsableSpace

Use it with care: Disk space is a volatile object.
Remember that filling your system disk until the last byte can disable the possibility to boot.
I agree with Walter: checking the free disk space is a bad programming style. On the other hand, I disagree: not checking the free disk space is even worse.
The only valid and clean solution: buy larger disks every month *and* do not process problems which could fill the diskspace.

Good luck, Jan

Subject: free disk space on network drive

From: Nils Tobias

Date: 21 Sep, 2010 21:11:20

Message: 9 of 18

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
> perhaps this helps and does not depend on the platform:
> FileObj = java.io.File('D:\');
> FileObj.getFreeSpace
> FileObj.getTotalSpace
> FileObj.getUsableSpace

Thanks a lot Jan, this worked and is exactly what I needed!!!

And as I said I'm not about to write onto every little bit available I just want to check if there are 10 or so GB left so that my function that creates and afterward deletes some 1-3GB can work properly.

In my opinion it's better to have a rough idea how much is left than starting a function that aborts somewhere in the code because it ran out of memory.

In any case I know I should first consider if I really need this function and all the data it creates.

Cheerz
NTK

Subject: free disk space on network drive

From: Jan Simon

Date: 21 Sep, 2010 21:31:20

Message: 10 of 18

Dear Nils,

I agree: It is *much* better to have a rough idea, as long as it is taken into account, that it is a rough idea only.

Kind regards, Jan

Subject: free disk space on network drive

From: Steve Amphlett

Date: 21 Sep, 2010 22:00:22

Message: 11 of 18

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i7b878$gl8$1@fred.mathworks.com>...
> Dear Nils,
>
> I agree: It is *much* better to have a rough idea, as long as it is taken into account, that it is a rough idea only.
>
> Kind regards, Jan

If it is a shared resource, work out its total capacity and assume 98% full. That's been my rule of thumb for the last 20 years.

Subject: free disk space on network drive

From: Walter Roberson

Date: 21 Sep, 2010 22:30:46

Message: 12 of 18

On 10-09-21 05:00 PM, Steve Amphlett wrote:

> If it is a shared resource, work out its total capacity and assume 98%
> full. That's been my rule of thumb for the last 20 years.

Yup, one of our groups routinely runs their 4 Tb partition over 90% full...

In a former life, I used to have to ask people to compress their 100 Kb files,
to try to free a megabyte of disk to swap our 512 Kb of RAM in to (it was a 4
CPU computer.)

In a different former life, we used to run 30 to 50 users on a 386.

Subject: free disk space on network drive

From: Igor

Date: 23 May, 2013 19:23:09

Message: 13 of 18

"Jan Simon" wrote in message <i7b51j$j0l$1@fred.mathworks.com>...
> perhaps this helps and does not depend on the platform:
> FileObj = java.io.File('D:\');
> FileObj.getFreeSpace
> FileObj.getTotalSpace
> FileObj.getUsableSpace

Huge thanks for this solution, Jan!
I've just added some wrapping code and uploaded it to fileexchange:

http://www.mathworks.com/matlabcentral/fileexchange/41904-disk-usage

Subject: free disk space on network drive

From: Bjorn Gustavsson

Date: 23 May, 2013 20:38:07

Message: 14 of 18

"Igor" wrote in message <knlqat$ri0$1@newscl01ah.mathworks.com>...
> "Jan Simon" wrote in message <i7b51j$j0l$1@fred.mathworks.com>...
> > perhaps this helps and does not depend on the platform:
> > FileObj = java.io.File('D:\');
> > FileObj.getFreeSpace
> > FileObj.getTotalSpace
> > FileObj.getUsableSpace
>
> Huge thanks for this solution, Jan!
> I've just added some wrapping code and uploaded it to fileexchange:
>
> http://www.mathworks.com/matlabcentral/fileexchange/41904-disk-usage
>
For all unix-like systems this is obviously the better solution:

[isOK,fs_res] = system('df');

Subject: free disk space on network drive

From: Jan Simon

Date: 26 May, 2013 16:04:11

Message: 15 of 18

Hi Bjorn Gustavsson,

> > > FileObj.getUsableSpace
> For all unix-like systems this is obviously the better solution:
> [isOK,fs_res] = system('df');

What are the benefits of df to the Java method getUsableSpace?

Kind regards, Jan

Subject: free disk space on network drive

From: Bjorn Gustavsson

Date: 26 May, 2013 21:09:11

Message: 16 of 18

Hello Jan,

"Jan Simon" wrote in message <kntbpr$foa$1@newscl01ah.mathworks.com>...
> Hi Bjorn Gustavsson,
>
> > > > FileObj.getUsableSpace
> > For all unix-like systems this is obviously the better solution:
> > [isOK,fs_res] = system('df');
>
> What are the benefits of df to the Java method getUsableSpace?
>
Maybe it boils down to preference and taste in the end. I prefer to use system commands available in the OS. When "atomic tools" like that are available I've taken the stance that they should be used, also old habits die hard, java wasn't available when I was young...

...hand me the cane...

Subject: free disk space on network drive

From: Igor

Date: 27 May, 2013 03:32:10

Message: 17 of 18

"Bjorn Gustavsson" <bjonr@irf.se> wrote in message <knttln$9j$1@newscl01ah.mathworks.com>...

> I prefer to use system commands available in the OS.

I've just aimed to provide a single solution that would work in most cases....
It's just simpler if it works on any platform...

Subject: free disk space on network drive

From: Bjorn Gustavsson

Date: 27 May, 2013 06:10:13

Message: 18 of 18

"Igor" wrote in message <knuk3p$p52$1@newscl01ah.mathworks.com>...
> "Bjorn Gustavsson" <bjonr@irf.se> wrote in message <knttln$9j$1@newscl01ah.mathworks.com>...
>
> > I prefer to use system commands available in the OS.
>
> I've just aimed to provide a single solution that would work in most cases....
> It's just simpler if it works on any platform...
>
Fair enough, but I will use the OS-tools for this type of tasks until I get the use-case where I have to get that question solved programatically.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us