Getting free space of a mapped network drive in GBs

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Getting free space of a mapped network drive in GBs

#1 Post by MKANET » 28 Apr 2012 18:06

Is there an easy way to get the amount of free space on a mapped network drive without getting something like Judago's str_math.bat http://judago.webs.com/mathsbyteconversion.htm involved?

Surely, there has to be an easy way to get free space shown in GBs (rounded to the hundredth place) without external utilities or batch files. It doesnt even look like str_math.bat rounds decimal places when dividing; it just cuts off the any numbers after a specified decimal place.


Code: Select all

cd /d N:\

FOR /F "tokens=*" %%i in ('dir ^| find "free"') do SET WinVer=%%i
FOR /F "tokens=1-3 delims=]-" %%A IN ("%WinVer%" ) DO (
SET freespace=%%A
)
set freespace=%freespace:~10,15%
set freespace=%freespace:,=%
for /f %%a in ('str_math.cmd %freespace% / 1073741824 2') do set GB=%%a
echo %GB%GB of free space

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Getting free space of a mapped network drive in GBs

#2 Post by aGerman » 28 Apr 2012 18:45

MKANET wrote:Surely, there has to be an easy way to get free space shown in GBs (rounded to the hundredth place) without external utilities or batch files.

No easy way.
Basically batch knows only 1 data type: a string of characters. Since cmd.exe came up M$ implemented also an integer data type. The casting from string to integer and vice versa happens on-the-fly. There are only 2 commands that can handle numeric values:
set /a for assigning integers and for calculations
if for numeric comparisions
Numeric values in batch are always integers in a 32 bit range. The greatest positive number is limited to 2^31 -1. If the result of a calculation is a floating point value then the decimales are stripped automatically.

I don't know the linked str_math.cmd but I assume it's a complicated combination of integer calculation and string manipulation. Perhaps you can implement these algorithms directly into your code.

Regards
aGerman

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Getting free space of a mapped network drive in GBs

#3 Post by trebor68 » 29 Apr 2012 11:00

If you will round a value you need one number more decimal in the result.
Please change the parameter to 3 drecimal in the command str_math.

value xxxx.yyz

If z have the value 5, 6, 7, 8 or 9 will round up.
result is yy+1
if yy+1 = 100 then xxxx+1
With z and the value 0, 1, 2, 3, 4 round down.

The batch will have three examples.

Code: Select all

@echo off
echo.
echo Rounding a value like xxxx.xxx to xxxx.xx.
echo.
call :round 1234.997
call :round 1234.567
call :round 1234.012
exit /b
:round
set num=%1
set num2=10%num:~-3%
set /a num2="(num2 %% 1000 + 5)/10"
set /a num1=num + num2/100
set /a num2=num2 %% 100
set num2=00%num2%
set hlp=%num1%.%num2:~-2%
echo   Value %1 is round to Value %hlp%


To understand the batch here one example:
value = 1234.567

10567
((10567 mod 1000) + 5) / 10
57.2
round 57
1234.57

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Getting free space of a mapped network drive in GBs

#4 Post by foxidrive » 29 Apr 2012 11:06

This works here to report how many GB free, not fully tested though.

Code: Select all

@echo off
:: %1 = c: or d: etc
setlocal
for /f "tokens=3" %%z in ('dir %1 /w ^|find "bytes free"') do (
for /f "tokens=1-6 delims=," %%a in ("%%z") do (
if not "%%d"=="" set gb=%%a GB free
if not "%%e"=="" set gb=%%a,%%b GB free
if not "%%f"=="" set gb=%%a,%%b,%%c GB free
if "%%d"=="" set gb=less than a GB free
)
)
echo %gb%
pause


Edited: altered so it doesn't parse every line
Last edited by foxidrive on 29 Apr 2012 13:37, edited 1 time in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Getting free space of a mapped network drive in GBs

#5 Post by aGerman » 29 Apr 2012 11:22

The conversion factor is 2^10 (1024) though.
1 KB = 1024 Bytes
1 MB = 1048576 Bytes
1 GB = 1073741824 Bytes

Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Getting free space of a mapped network drive in GBs

#6 Post by foxidrive » 29 Apr 2012 11:27

On my post? A GB is 1,000,000,000 bytes.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Getting free space of a mapped network drive in GBs

#7 Post by aGerman » 29 Apr 2012 12:28

I assume you're right (according to the SI standard). Even if GB is often used for 1,073,741,824 bytes it should better be marked as GiB in that case.
I refer the first post where MKANET needs to divide by 1,073,741,824.

Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Getting free space of a mapped network drive in GBs

#8 Post by foxidrive » 29 Apr 2012 13:32

MKANET also said he wants a hundredth place. I didn't understand that :) Maybe he wants a single decimal place. Is that in GiB or GB?


Regardless, I know of the differences and I'm merely commenting that my post provides power of ten GB and MKANET is free to use it or not, of course.
It's a simple way to get a ballpark figure, and works in my tests.

Aacini
Expert
Posts: 1886
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Getting free space of a mapped network drive in GBs

#9 Post by Aacini » 29 Apr 2012 14:45

Wow! I made an interesting discovery today :D but first a very brief introduction:

In assembly language the multiplication of two 32-bits registers give a 64-bits wide result in two registers; for example, 123 times 100:

Code: Select all

mov   eax,123      ;EAX = 123
mov   ebx,100      ;EBX = 100
mul   ebx          ;EAX*EBX -> EDX:EAX = 12300

High part of the result goes into EDX and low part in EAX (EDX:EAX notation). The division is exactly the opposite procedure: divide EDX:EAX 64-bits number by a 32-bits divisor, result is EAX=quotient and EDX=remainder. This way, before the division of two 32-bits numbers you must clear the high part of the dividend:

Code: Select all

mov   edx,0       ;clear high part of EDX:EAX
mov   eax,12345   ;EDX:EAX = 12345
div   ebx         ;EAX = 123, EDX = 45

When I evaluate a large expression I used to clear EDX just once. I can preserve some precision when a multiplication is followed by a division:

Code: Select all

mov   eax,1999999999   ;EAX = 1999999999
mov   ebx,1000000000   ;EBX = 1000000000
mul   ebx              ;EDX:EAX = 1999999999000000000
add   eax,ebx          ;EDX:EAX = 2000000000000000000
div   ebx              ;EAX = 2000000000

This way if I carefully choose the order of operations and use the right factors, I can get a correct result of an expression with partial results that exceed a 32-bits number.

Well, it seems that SET /A command ACHIEVE THE SAME THING! :shock: :!:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
if "%1" neq "" cd /D %1
for /F "tokens=3" %%a in ('dir ^| find "free"') do set freeSpace=%%a
set freeSpace=%freeSpace:,=%
set highPart=%freeSpace:~0,-6%
set lowPart=%freeSpace:~-6%
for /L %%a in (1,1,5) do if "!lowPart:~0,1!" equ "0" set lowPart=!lowPart:~1!
set /A GB=(highPart*1000000+lowPart)/1073741824, HH=((highPart*1000000+lowPart)%%1073741824/1000000+5)/10
echo %GB%.%HH% GB of free space


Interesting! Isn't it? 8) This feature may be used in several other instances... :wink:

Antonio

EDIT: Although my description about assembly trick is correct, my conclusion about SET /A command is not... :oops: :cry:

However, I posted below a new method that correctly works. :wink:
Last edited by Aacini on 29 Apr 2012 22:50, edited 1 time in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Getting free space of a mapped network drive in GBs

#10 Post by aGerman » 29 Apr 2012 15:33

Hmm, doesn't work for me. Because the output of my dir command differs I simplified the code.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
:: if "%1" neq "" cd /D %1
:: for /F "tokens=3" %%a in ('dir ^| find "frei"') do set freeSpace=%%a
:: set freeSpace=%freeSpace:.=%

set freeSpace=220192894976
set highPart=%freeSpace:~0,-6%
set lowPart=%freeSpace:~-6%

set highPart
set lowPart

for /L %%a in (1,1,5) do if "!lowPart:~0,1!" equ "0" set lowPart=!lowPart:~1!
set /A GB=(highPart*1000000+lowPart)/1073741824, HH=((highPart*1000000+lowPart)%%1073741824/1000000+5)/10
echo %GB%.%HH% GB of free space

Code: Select all

highPart=220192
lowPart=894976
1.8 GB of free space

Regards
aGerman

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Getting free space of a mapped network drive in GBs

#11 Post by MKANET » 29 Apr 2012 16:41

I always thought GB was 1024 ^3 (abbreviation for Gigabytes). Anyway, I was looking for the result in whatever the standard is for the GB abbreviation. Also, most instances ill use this code is when running low on free disk space, so, ill need the result rounded to the hundredth decimal place to be a little more precise (since I don't want to display in megabytes.

I always thought GB was 1024 ^3 (abbreviation for Gigabytes). Anyway, I was looking for the result in whatever the standard is for the GB abbreviation. Also, most instances ill use this code is when running low on free disk space, so, ill need the result rounded to the hundredth decimal place to be a little more precise (since I don't want to display in megabytes.

I just tried the latest code above. When I uncommented the above lines to get a the real freespace value, it doesnt look like it's working correctly:

Freespace string=158367703040

highPart=158381
lowPart=920256
0.-52 GB of free space ?


I modified my original batch file to round numbers with the code mentioned above. It looks like it's working correctly:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for /F "tokens=3" %%a in ('dir ^| find "free"') do set freeSpace=%%a
set freeSpace=%freeSpace:,=%
for /f %%a in ('str_math.cmd %freespace% / 1073741824 3') do set GB=%%a
call :round %GB%

echo %freespace%GB of free space

goto end
:round
set num=%1
set num2=10%num:~-3%
set /a num2="(num2 %% 1000 + 5)/10"
set /a num1=num + num2/100
set /a num2=num2 %% 100
set num2=00%num2%
set hlp=%num1%.%num2:~-2%
set freespace=%hlp%
exit /b
:end


Correct Result:
147.49GB of free space

If I can get it to work correctly without using str_math.cmd, that would be great.
Last edited by MKANET on 29 Apr 2012 18:32, edited 5 times in total.

Aacini
Expert
Posts: 1886
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Getting free space of a mapped network drive in GBs

#12 Post by Aacini » 29 Apr 2012 22:42

Oops! My previous method was wrong... :oops:

However, the method below is correct:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
:: if "%1" neq "" cd /D %1
:: for /F "tokens=3" %%a in ('dir ^| find "bytes free"') do set freeSpace=%%a
:: set freeSpace=%freeSpace:,=%

set freeSpace=%1

set highPart=%freeSpace:~0,-6%
set lowPart=%freeSpace:~-6%
set /A KB=highPart/1024*1000000, borrow=highPart%%1024
if %borrow% gtr 0 (
   set lowPart=%borrow%%lowPart%
) else (
   for /L %%a in (1,1,5) do if "!lowPart:~0,1!" equ "0" set lowPart=!lowPart:~1!
)
set /A KB+=lowPart/1024, GB=KB/1048576, HH=KB%%1048576*100/1048576
if %HH% lss 10 set HH=0%HH%
echo %GB%.%HH% GB of free space

Code: Select all

>freespace 220192894976
205.07 GB of free space

>freespace 158367703040
147.49 GB of free space

>freespace 2199023254528
2047.99 GB of free space

>freespace 1073741824
1.00 GB of free space

>freespace 1073741823
0.99 GB of free space

This method work with free spaces up to 2,199,023,254,528 bytes maximum (2,147,483,647 KB, almost 2,048 GB).

@MKANET: If you want hundredths be rounded up, change last SET command by these ones:

Code: Select all

set /A KB+=lowPart/1024, GB=KB/1048576, HH=KB%%1048576*1000/1048576
if %HH% lss 995 set /A HH+=5
set /A HH/=10


Antonio

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Getting free space of a mapped network drive in GBs

#13 Post by MKANET » 29 Apr 2012 23:09

This is absolutely perfect!! Dont choke me, but is there a relatively easy way to have the output show in MB's too based on this same engine?

Aacini
Expert
Posts: 1886
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Getting free space of a mapped network drive in GBs

#14 Post by Aacini » 30 Apr 2012 00:21

Code: Select all

set /A KB+=lowPart/1024, MB=KB/1024, HH=MB%%1024*100/1024
. . . .
echo %MB%.%HH% MB of free space

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Getting free space of a mapped network drive in GBs

#15 Post by dbenham » 01 May 2012 06:27

:shock: Very cool and impressive Antonio 8)

I'll have to study the mechanics of how it works so I can use the technique in the future.

Based on my limited understanding of your explanation, there is no similar method for addition/subtraction :?:
This only works for multiplication/division :?:

Dave Benham

Post Reply