Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find the percentage of free space for mapped drives?

Is it possible to programatically find the free space available in mapped drives?

How to find the percentage of free space in your drive using ms-dos.
It may be easy to find the free space for a drive in ur hard disc but i need to find the free-space of mapped drives.

I have mapped some file servers in my systems.

It is possible to see this in My Computer, but how do show it in a command prompt?

like image 936
Arunachalam Avatar asked Mar 29 '10 15:03

Arunachalam


People also ask

How do you calculate free space percentage?

And we have good news for you: determining the percentage of free disk space is almost as easy. That's because the percentage of free disk space requires just a simple calculation: you take the available disk space and divide it by the total size of the drive.

How do I see free space on my network drive?

How to check the available disk space / usage. In windows explorer, browse to the network share, then to the folder that you want to check the disk usage, right client on the folder and select properties. In the properties window, click on the "OES Info" tab, and check the "Space Available" field.

What is the percentage of free space on the hard drive?

The 15% Rule of Thumb for Mechanical Hard Drives You'll commonly see a recommendation that you should leave 15% to 20% of a drive empty. That's because, traditionally, you needed at least 15% free space on a drive so Windows could defragment it.

How do you find the percentage to drive?

How Do we Calculate Percentage? Percentage can be calculated by dividing the value by the total value, and then multiplying the result by 100. The formula used to calculate percentage is: (value/total value)×100%.


2 Answers

(Taken from an old answer of mine over at Server Fault)

The easiest way to reliably get at the free disk space is using WMI. When trying to parse the output of dir you get all kinds of funny problems, at the very least with versions of Windows in other languages. You can use wmic to query the free space on a drive:

wmic logicaldisk where "DeviceID='C:'" get FreeSpace

This will output something like

FreeSpace
197890965504

You can force this into a single line by adding the /format:value switch:

> wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value

FreeSpace=197890965504

There are a few empty lines there, though (around three or four) which don't lend themselves well for processing. Luckily the for command can remove them for us when we do tokenizing:

for /f "usebackq delims== tokens=2" %x in (`wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do set FreeSpace=%x

The nice thing here is that since we're only using the second token all empty lines (that don't have a second token) get ignored.

Remember to double the % signs when using this in a batch file:

for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do set FreeSpace=%%x

You can now use the free space which is stored in the environment variable %FreeSpace%.


Getting percentages now is a little tricky since batch files only support 32-bit integers for calculation. However, you probably don't need to calculate this to the byte; I think megabytes are quite enough:

for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do set FreeSpace=%%x
for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get Size /format:value`) do set Size=%%x
set FreeMB=%FreeSpace:~0,-6%
set SizeMB=%Size:~0,-6%
set /a Percentage=100 * FreeMB / SizeMB
echo C: is %Percentage% % free

This should work unless your volumes get larger than 20 TiB.

like image 126
Joey Avatar answered Sep 28 '22 10:09

Joey


You need GetDiskFreeSpaceEx. Works with drives, mapped drives, etc.

ULARGE_INTEGER free;
ULARGE_INTEGER total;
ULARGE_INTEGER totalFree;
BOOL           ok;

ok = GetDiskSpaceFreeEx(path, &free, &total, &totalFree);
if (ok)
{
// do your sums here, then printf the result
}
like image 36
Stephen Kellett Avatar answered Sep 28 '22 12:09

Stephen Kellett