Getting free space of a mapped network drive in GBs

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

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

#16 Post by Aacini » 01 May 2012 12:56

dbenham wrote: :shock: Very cool and impressive Antonio 8)
Thanks a lot, Dave! :D
dbenham wrote:... there is no similar method for addition/subtraction :?:
The example below add two numbers up to 18 digits each:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set A=%1
set B=%2
set highA=%A:~0,-9%& set lowA=%A:~-9%
set highB=%B:~0,-9%& set lowB=%B:~-9%
for /L %%a in (1,1,8) do (
   if "!lowA:~0,1!" equ "0" set lowA=!lowA:~1!
   if "!lowB:~0,1!" equ "0" set lowB=!lowB:~1!
)
set /A low=lowA+lowB, carry=low/1000000000, low%%=1000000000, high=highA+highB+carry
if %high% gtr 0 (
   set low=00000000%low%
   echo %high%!low:~-9!
) else (
   echo %low%
)

Code: Select all

>AddBigNums 0 1
1

>AddBigNums 999999999 1
1000000000

>AddBigNums 999999999 999999999
1999999998

>AddBigNums 999999999999999999 1
1000000000000000000

>AddBigNums 123456789012345678 987654321098765432
1111111110111111110

You may also achieve operations with fractional parts implying the decimal point position (that is similar to the examples above). I stated a brief introduction to fractional operations at this post.

If you want to achieve any sequence of operations on big numbers, then original numbers must be split in groups of just 4 digits (quads) because 9999*9999 is the maximum result that fits in a 32-bits number. I wrote an (unfinished yet) library of BigNum functions; I copied this part from it:

Code: Select all

rem The format of a BigNum variable is:
rem
rem varInt, varInt-1, ..., var0 contain integer 4-digits groups (Quads)
rem var-1, var-2, ..., var-Frac contain fractional 4-digits groups (Quads)
rem var=Sign,Int,-Frac,-Last (BigNum descriptor)
rem     Sign: 0 for positive, 1 for negative
rem     Int:  number of integer Quads minus 1
rem     Frac: number of non-zero fractional Quads (with minus sign)
rem     Last: number of total fractional Quads (with minus sign)
rem
rem For example:  call SetBigNum var=12340008.43215432654376  result in:
rem var=0,1,-4,-4  var1=1234  var0=8  var-1=4321  var-2=5432  var-3=6543  var-4=7600

Let me know if I may help you any further.

Antonio

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

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

#17 Post by miskox » 01 May 2012 17:43

Aacini, very good.
I have one improvement:

this code

Code: Select all

:: set freeSpace=%freeSpace:.=%


should be added just below the

Code: Select all

:: set freeSpace=%freeSpace:,=%


So the code would delete , and . from the string.

We use dots as a thousand separators and not commas. Also decimal point is replaced by comma. (COBOL has this statement: DECIMAL POINT IS COMMA.).

Saso

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

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

#18 Post by MKANET » 12 May 2012 21:47

It looks like there might be something wrong with MB conversion. When the original value in bytes is too low, converted MB value is rounded off to a whole number:

freespace=5999999
I get 11.01MB's

freespace=4999999
The result is 9.00MB's

freespace=4494567
The result is 8.00MB's

freespace=3599999
The result is 6.00MB's

Code: Select all

:MBConvert
set freespace=5999999
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%
set freespaceGB=%GB%.%HH%
set /A KB+=lowPart/1024, MB=KB/1024, HH=MB%%1024*100/1024
if %HH% lss 10 set HH=0%HH%
set freespaceMB=%MB%.%HH%
if %MB% GTR 999 (set freespace=%freespaceGB%GB's
) ELSE (
set freespace=%freespaceMB%MB's
)

Post Reply