Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

free disk space on network drive

14 views
Skip to first unread message

Albert

unread,
Jun 16, 2010, 5:39:04 PM6/16/10
to
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

Walter Roberson

unread,
Jun 16, 2010, 5:47:36 PM6/16/10
to
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.

Albert

unread,
Jun 16, 2010, 6:09:03 PM6/16/10
to
Walter Roberson <robe...@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?

Walter Roberson

unread,
Jun 16, 2010, 8:19:40 PM6/16/10
to

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".)

Albert

unread,
Jun 17, 2010, 6:49:04 PM6/17/10
to
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.

Walter Roberson

unread,
Jun 17, 2010, 11:21:24 PM6/17/10
to
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.

Nils Tobias

unread,
Sep 21, 2010, 6:42:04 AM9/21/10
to
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

Jan Simon

unread,
Sep 21, 2010, 4:37:07 PM9/21/10
to
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

Nils Tobias

unread,
Sep 21, 2010, 5:11:20 PM9/21/10
to
"Jan Simon" <matlab.T...@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

Jan Simon

unread,
Sep 21, 2010, 5:31:20 PM9/21/10
to
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

Steve Amphlett

unread,
Sep 21, 2010, 6:00:22 PM9/21/10
to
"Jan Simon" <matlab.T...@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.

Walter Roberson

unread,
Sep 21, 2010, 6:30:46 PM9/21/10
to
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.

Igor

unread,
May 23, 2013, 3:23:09 PM5/23/13
to
"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

Bjorn Gustavsson

unread,
May 23, 2013, 4:38:07 PM5/23/13
to
"Igor" wrote in message <knlqat$ri0$1...@newscl01ah.mathworks.com>...
For all unix-like systems this is obviously the better solution:

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

Jan Simon

unread,
May 26, 2013, 12:04:11 PM5/26/13
to
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

Bjorn Gustavsson

unread,
May 26, 2013, 5:09:11 PM5/26/13
to
Hello Jan,

"Jan Simon" wrote in message <kntbpr$foa$1...@newscl01ah.mathworks.com>...
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...

Igor

unread,
May 26, 2013, 11:32:10 PM5/26/13
to
"Bjorn Gustavsson" <bj...@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...

Bjorn Gustavsson

unread,
May 27, 2013, 2:10:13 AM5/27/13
to
"Igor" wrote in message <knuk3p$p52$1...@newscl01ah.mathworks.com>...
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.
0 new messages