Can I divide my result?
Moderator: DosItHelp
Can I divide my result?
Hello, I am using a script to find the available bytes on a drive and spit it out into a .txt file. As I understand it math functionality is limited but I am wondering if there is a way to divide the answer I'm getting. I am currently using this script
@echo off & setLocal EnableDELAYedeXpansion
for /f "tokens=3 delims= " %%a in ('dir \ ^| find "bytes free"') do (
set G=%%a
set G=!G:,=!
set G=!G:~0,-9!
echo.!G! >> FreeSpace.txt
)
I am currently getting the available space in bytes, but only the raw GB, for example if there are 999,579,469,824 bytes free my FreeSpace.txt will read 999, which I would like to divide by 1074 so my .txt could read 0.930 TB Free. I appreciate any help I'm given and I hope I've given sufficient information about my issue. Thanks in advance.
@echo off & setLocal EnableDELAYedeXpansion
for /f "tokens=3 delims= " %%a in ('dir \ ^| find "bytes free"') do (
set G=%%a
set G=!G:,=!
set G=!G:~0,-9!
echo.!G! >> FreeSpace.txt
)
I am currently getting the available space in bytes, but only the raw GB, for example if there are 999,579,469,824 bytes free my FreeSpace.txt will read 999, which I would like to divide by 1074 so my .txt could read 0.930 TB Free. I appreciate any help I'm given and I hope I've given sufficient information about my issue. Thanks in advance.
Re: Can I divide my result?
Batch files are limited to 32 bit integers. So the largest integer you can work with is 2147483647.
There is a batch file made by Judago that works around this limitation. Look for Divide.bat on this page.
http://judago.webs.com/batchfiles.htm
There is a batch file made by Judago that works around this limitation. Look for Divide.bat on this page.
http://judago.webs.com/batchfiles.htm
Re: Can I divide my result?
Thanks, that seems a lot more in-depth than I was thinking. I'd just like to divide !G! by 1074?
Re: Can I divide my result?
I see you still try to get it in GB value,
I tried what you did but the problem is in the comma between each 3 digits, I can't remove it and get the numbers without a specific length of this numbers
so if the total free space in a drive is x,xxx,xxx,xxx and in another is x,xxx,xxx the code will work for one but not for the other

I tried what you did but the problem is in the comma between each 3 digits, I can't remove it and get the numbers without a specific length of this numbers
so if the total free space in a drive is x,xxx,xxx,xxx and in another is x,xxx,xxx the code will work for one but not for the other
Re: Can I divide my result?
Batch math only works with integers.
Re: Can I divide my result?
goymerac wrote:Thanks, that seems a lot more in-depth than I was thinking. I'd just like to divide !G! by 1074?
So what don't you understand about 32 bit integers. The example you provided is larger than the largest 32 bit integer that SET /A can handle.
You have two choices. You either use the code I provided to do it in pure batch or you pass the values to a vbscript.
Re: Can I divide my result?
goymerac wrote:I am currently getting the available space in bytes, but only the raw GB, for example if there are 999,579,469,824 bytes free my FreeSpace.txt will read 999, which I would like to divide by 1074 so my .txt could read 0.930 TB Free.
Think you've got the math wrong. In decimal units 999,579,469,824 bytes is (after rounding) 1,000 GB or 1 TB. In binary units (http://physics.nist.gov/cuu/Units/binary.html) it's 930 GiB or 0.909 TiB.
As said already, the builtin set/a arithmetic only works up to 2 GiB values, so for numbers above that you'll need to basically do it by hand, or borrow some batch code to do it for you.
Liviu
Re: Can I divide my result?
Squashman wrote:goymerac wrote:Thanks, that seems a lot more in-depth than I was thinking. I'd just like to divide !G! by 1074?
So what don't you understand about 32 bit integers. The example you provided is larger than the largest 32 bit integer that SET /A can handle.
You have two choices. You either use the code I provided to do it in pure batch or you pass the values to a vbscript.
I tried this code but with my little knowledge of batch code I could get it to work. The command prompt would just close or display the code.
@ECHO OFF
if "%~1"=="/?" (
echo.&echo USAGE:&echo.
echo "%~0" largenumber smallnumber [variablename] [places]
echo "%~0" largenumber / smallnumber [variablename] [places]
echo.&echo "largenumber" can floating point "smallnumber" can't.
echo Only the first [places] decimal places are used for input or
echo output. if [places] is omitted then the default of 8 is used.
echo.&echo To specify [places] with out [variablename] pass an empty set.
echo Below is an example:
echo.&echo "%~0" 46546545464.123456789 / 1024 "" 9&echo.
echo.&echo "smallnumber" must be below 2097153, "largenumber" can have
echo hundreds of places.&echo.&echo -Judago 2009/2010
exit /b 0
)
SETLOCAL ENABLEDELAYEDEXPANSION
set error=Invalid Input
if "%~1"=="" goto error
if "%~2"=="/" shift /2
if "%~2"=="" goto error
for /f "delims=1234567890." %%a in ("%~1%~2") do goto error
set divisor=%~2
set error=divisor must be whole
if not "!divisor!"=="!divisor:.=!" goto error
if !divisor! gtr 2097152 (
set error=divisor too large, limited to: 2097152
goto error
)
set dplace=%~4
if not defined dplace set dplace=8
for /f "delims=1234567890." %%a in ("%~4") do set dplace=8
set input=0%~1
for /l %%a in (1 1 %dplace%) do set input=!input!0
set error=Divide by zero
if "!divisor:0=!"=="" goto error
set chunk=
set total=
set /a fpos=dplace + 1
:isfloat
if not "!input:.=!"=="!input!" (
if not "!input:~-%fpos%,1!"=="." (
set input=!input:~0,-1!
goto isfloat
) else (
set input=!input:.=!
)
)
:split
if not "%input:~3%"=="" (
set /a chunk+=1
set input!chunk!=%input:~-3%
set input=%input:~0,-3%
if defined input goto split
) else (
set /a chunk+=1
set input!chunk!=%input%
)
:loop
if defined input%chunk% (
if "!input%chunk%:~0,1!"=="0" (
set input%chunk%=!input%chunk%:~1!
goto loop
)
) else (
set input%chunk%=0
goto pad
)
set chunkresult=0
:divide
If !input%chunk%! geq !divisor! (
If !input%chunk%! geq !divisor!000 (
set /a input%chunk%-=!divisor!000
set /a chunkresult+=1000
goto divide
) else (
If !input%chunk%! geq !divisor!00 (
set /a input%chunk%-=!divisor!00
set /a chunkresult+=100
goto divide
) else (
If !input%chunk%! geq !divisor!0 (
set /a input%chunk%-=!divisor!0
set /a chunkresult+=10
goto divide
) else (
set /a input%chunk%-=!divisor!
set /a chunkresult+=1
goto divide
)
)
)
)
:pad
if "!chunkresult:~2,1!"=="" set chunkresult=0!chunkresult!
if "!chunkresult:~2,1!"=="" set chunkresult=0!chunkresult!
set total=%total%%chunkresult%
set chunkresult=0
if %chunk% gtr 0 (
set /a chunk-=1
if !input%chunk%! gtr 0 (
set carry=!input%chunk%!
for %%a in (!chunk!) do set input!chunk!=!carry!!input%%a!
)
)
if %chunk% gtr 0 goto loop
if not defined total set total=0
if %dplace% gtr 0 set total=!total:~0^,-%dplace%!.!total:~-%dplace%!
:finish
if "%total:~0,1%"=="0" if not "%total:~1%"=="" set total=%total:~1%&&goto finish
if "%total:~0,1%"=="." set total=0%total%
if not "%~3"=="" (
endlocal
set %~3=%total%
) else (
echo %total% - Leftover:!input%chunk%! ^(this isn't always the mod result^)
endlocal
)
exit /b 0
:Error
1>&2 echo %error% - See "%~0 /?"
endlocal
exit /b 1
Re: Can I divide my result?
goymerac wrote:The command prompt would just close or display the code.
When doing what?? It generally helps if you provide some context.
I haven't really used judago's routine myself, but it looks like it's doing what you want. Save the code as, for example, judago-divide.bat, then save this as b2kmgt.cmd.
Code: Select all
@echo off
setlocal
set "B=%~1"
echo %B% B
call judago-divide %B% 1024 K
echo = %K% KiB
call judago-divide %K% 1024 M
echo = %M% MiB
call judago-divide %M% 1024 G
echo = %G% GiB
call judago-divide %G% 1024 T
echo = %T% TiB
endlocal
goto :eof
Code: Select all
C:\tmp>b2kmgt 999579469824
999579469824 B
= 976151826.00000000 KiB
= 953273.26757812 MiB
= 930.93092536 GiB
= 0.90911223 TiB
Re: Can I divide my result?
I'm working in the command prompt and driving to the appropriate drive and pasting the code in there and it just closes.
when my command prompt looks like this
E:\>
I am pasting judago's code
I tried pasting your code
@echo off
setlocal
set "B=%~1"
echo %B% B
call judago-divide %B% 1024 K
echo = %K% KiB
call judago-divide %K% 1024 M
echo = %M% MiB
call judago-divide %M% 1024 G
echo = %G% GiB
call judago-divide %G% 1024 T
echo = %T% TiB
endlocal
goto :eof
but the command prompt just shows the code and doesn't actually do anything.
Your output results are exactly what I'm looking for.
when my command prompt looks like this
E:\>
I am pasting judago's code
I tried pasting your code
@echo off
setlocal
set "B=%~1"
echo %B% B
call judago-divide %B% 1024 K
echo = %K% KiB
call judago-divide %K% 1024 M
echo = %M% MiB
call judago-divide %M% 1024 G
echo = %G% GiB
call judago-divide %G% 1024 T
echo = %T% TiB
endlocal
goto :eof
but the command prompt just shows the code and doesn't actually do anything.
Your output results are exactly what I'm looking for.
Re: Can I divide my result?
In the folder containing your bat files, open a CMD prompt and type
DIR /b
Then check that the .bat file names don't have a .bat.txt on them or something odd.
DIR /b
Then check that the .bat file names don't have a .bat.txt on them or something odd.
Re: Can I divide my result?
Just double checked and they're all .bat and .cmd
Re: Can I divide my result?
You don't paste the code at the cmd prompt. You paste it in your editor, then save it as a .cmd or .bat file, which you then execute at the cmd prompt.goymerac wrote:when my command prompt looks like this
E:\>
I am pasting judago's code
I tried pasting your code
That's very unlikely. Whatever you did, the command prompt should show either some results, or some errors. Copy those, and post those.goymerac wrote:but the command prompt just shows the code and doesn't actually do anything.
Open the command prompt at E:\ or wherever you are testing this, and enter "type judago-divide.bat" then "type b2kmgt.cmd" at that prompt. This should display the contents of the two batch files. If it doesn't, then you don't really have the files you think you have there.goymerac wrote:Just double checked and they're all .bat and .cmd
Liviu
Re: Can I divide my result?
I have tested following code.
The command CALL have two parameters:
param1 - the free size without the comma (in some countrys is there a point)
param2 - how many time you will divide 1024; 1 for KiB, 2 for MiB, 3 for GiB and 4 for TiB
To understand where the variables are found in the variable RESULT: aaa,bbb,ccc,ddd,eee.fff
The command CALL have two parameters:
param1 - the free size without the comma (in some countrys is there a point)
param2 - how many time you will divide 1024; 1 for KiB, 2 for MiB, 3 for GiB and 4 for TiB
Code: Select all
@echo off
for /f "tokens=3" %%a in ('dir ^| find "Bytes free"') do set wert=%%a
echo %wert% Bytes
set wert=%wert:,=%
call :div1024 %wert% 4
::
goto :eof
:div1024
set value=%1
set count=%2
set aaa=%value:~0,-12%
if %aaa%#==# set aaa=0
set bbb=1000%value:~-12,-9%
set /a bbb="bbb %% 1000"
set ccc=1000%value:~-9,-6%
set /a ccc="ccc %% 1000"
set ddd=1000%value:~-6,-3%
set /a ddd="ddd %% 1000"
set eee=1000%value:~-3%
set /a eee="eee %% 1000"
set fff=0
::
:divnext
set /a zzz=aaa
set /a aaa=zzz / 1024
set /a zzz="(zzz %% 1024) * 1000 + bbb
set /a bbb=zzz / 1024
set /a zzz="(zzz %% 1024) * 1000 + ccc
set /a ccc=zzz / 1024
set /a zzz="(zzz %% 1024) * 1000 + ddd
set /a ddd=zzz / 1024
set /a zzz="(zzz %% 1024) * 1000 + eee
set /a eee=zzz / 1024
set /a zzz="(zzz %% 1024) * 1000 + fff
set /a fff=zzz / 1024
set /a count-=1
if not %count%==0 goto :divnext
set aaa=000%aaa%
set bbb=000%bbb%
set ccc=000%ccc%
set ddd=000%ddd%
set eee=000%eee%
set fff=000%fff%
set result=%aaa:~-3%,%bbb:~-3%,%ccc:~-3%,%ddd:~-3%,%eee:~-3%
:: remove the number zero in front of the result
if %result:~0,4%==000, set result=%result:~4%
if %result:~0,4%==000, set result=%result:~4%
if %result:~0,4%==000, set result=%result:~4%
if %result:~0,4%==000, set result=%result:~4%
if %result:~0,1%==0 set result=%result:~1%
if %result:~0,1%==0 set result=%result:~1%
::
:: next row: GEQ 3 for GiB and TiB with three digits after the point
if %2 geq 3 set result=%result%.%fff:~-3%
::
for /f "tokens=%2" %%a in ("KiB MiB GiB TiB") do set zzz=%%a
echo Result: %result% %zzz%
goto :eof
To understand where the variables are found in the variable RESULT: aaa,bbb,ccc,ddd,eee.fff