Page 1 of 1
Capturing Math batch's output to a variable
Posted: 29 Apr 2013 13:46
by Ranguna173
Hello everyone.
So I've been searching on the web for a math script I and found one, you guys can check it out
here or
here.
The batch works perfecly, it can process numbers bigger than 10 digits and can also use decimal numbers.
The problem is that I can't capture the output when I start it with another batch.
I use this to start the file in the first script:
Code: Select all
start /wait /i %ComSpec% /c STR_MATH.bat (commands)
But I want to get the output of STR_MATH.bat to the first script.
Documentation of the math batch says that it can be captured with a "for /f" command but since I can't get my head around those "for"'s I can't seem to code one.
I've tried:
Code: Select all
start /wait /i %ComSpec% /c STR_MATH.bat (commands) > file
< file set /p output=
But apperantly the math script won't save anything into files.
I would like a code that captures the output of STR_MATH.bat
Thanks for reading and Please Help.
Re: Capturing Math batch's output to a variable
Posted: 29 Apr 2013 14:01
by Endoro
Ranguna173 wrote:I would like a code that captures the output of STR_MATH.bat
Please explain the output, I don't know "STR_MATH.bat". Thank you.
Re: Capturing Math batch's output to a variable
Posted: 29 Apr 2013 14:57
by Ranguna173
Endoro wrote:Ranguna173 wrote:I would like a code that captures the output of STR_MATH.bat
Please explain the output, I don't know "STR_MATH.bat". Thank you.
SRT_MATH.bat is
this file.
The output is the result of the mathematical operation you do when you use
SRT_MATH.batEx:.
Output in the CMD window:
Re: Capturing Math batch's output to a variable
Posted: 29 Apr 2013 15:12
by miskox
This works for me:
Code: Select all
start /wait /i %ComSpec% /c "STR_MATH.bat (commands) > file"
< file set /p output=
Note quotes above.
Your redirection was for a start command and not for the STR_MATH.bat command. That's why you have to put it in quotes.
Hope this helps,
Saso
Re: Capturing Math batch's output to a variable
Posted: 29 Apr 2013 15:15
by Ranguna173
miskox wrote:This works for me:
Code: Select all
start /wait /i %ComSpec% /c "STR_MATH.bat (commands) > file"
< file set /p output=
Note quotes above.
Your redirection was for a start command and not for the STR_MATH.bat command that's why you have to put it in quotes.
Hope this helps,
Saso
Could your remove the colors, apparently they don't work inside the Code tag.
EDIT: Thanks
Re: Capturing Math batch's output to a variable
Posted: 29 Apr 2013 15:24
by Ranguna173
miskox wrote:This works for me:
Code: Select all
start /wait /i %ComSpec% /c "STR_MATH.bat (commands) > file"
< file set /p output=
Note quotes above.
Your redirection was for a start command and not for the STR_MATH.bat command. That's why you have to put it in quotes.
Hope this helps,
Saso
Ohh, now I see why it wasn't saveing to file, thanks man.
Didn't need a "for" loop after all, awesome.
Thanks again for all your help
Re: Capturing Math batch's output to a variable
Posted: 29 Apr 2013 15:34
by Endoro
Code: Select all
@echo off & setlocal
for /f %%i in ('SRT_MATH.bat 2 x 2') do set /a res=%%i
echo %res%
Re: Capturing Math batch's output to a variable
Posted: 29 Apr 2013 15:50
by Ranguna173
Endoro wrote:Code: Select all
@echo off & setlocal
for /f %%i in ('SRT_MATH.bat 2 x 2') do set /a res=%%i
echo %res%
That works too but you can't use numbers that are bigger than 10 digits with that for, it'll say something like "numbers are limited at a precision of 32 bit".
But that would probably be faster if you plan on using SRT_MATH.bat in a loop.
Re: Capturing Math batch's output to a variable
Posted: 29 Apr 2013 17:07
by foxidrive
This should work as well, without the start.
Re: Capturing Math batch's output to a variable
Posted: 30 Apr 2013 11:46
by Ranguna173
Which one do you guys this is faster ?
Code: Select all
:a
(Other Codes)
start /wait /i %ComSpec% /c "SRT_MATH.bat 2 x 2 > file"
< file set /p a=
(Other Codes)
goto a
or
Code: Select all
:a
(Other Codes)
>file.txt call SRT_MATH.bat 2 x 2
< file set /p a=
(Other Codes)
goto a
I tested the time on both of them and got 0:00:13.02 on the first code and 0:0014.21 on the second one.
But my finger isn't accurate so the results aren't really reliable.
Re: Capturing Math batch's output to a variable
Posted: 13 May 2013 00:09
by carlsomo
I use Dale Holt's Math.exe that works in 32 and 64 bit windows command line and handles just about any mathematical expression.
Then I wrote math.bat for command line assignment of variables:
Dale Holt's MATH.ZIP can be downloaded here:
http://www.myvirtualnetwork.com/mklotz/#mathCode: Select all
::Math.bat
@echo off&goto :start
:Math.bat "EnVar"="expresion..."
echo.Usage: Math.bat "Envar"="mathematical expression"
echo.ie. Math.bat "Compensation"="%%Hours_worked%%*%%HourlyRate%%"
echo.The envirenment variable will contain the result
echo.Check for non numeric error messages in EnVar
echo.Returns errorlevel from Dale Holt's Math.exe program
math.exe
exit /b %errorlevel%
:start
SetLocal EnableExtensions
set pp1=%~1&set pp2=%~2
if not defined pp1 call :Math.bat&endlocal&exit /b 1
if not defined pp2 call :Math.bat&endlocal&exit /b 1
for /f "usebackq tokens=*" %%a in (`c:\utilities\math.exe "%~2%~3%~4%~5%~6%~7%~8%~9"`) do (
set/a err=%errorlevel%
set "result=%%a"
)
set "result=%result:,=%"
EndLocal&call set "%~1=%result%"&exit /b %err%
Re: Capturing Math batch's output to a variable
Posted: 13 May 2013 09:08
by Aacini
Ranguna173 wrote:Which one do you guys this is faster ? ...
I think the fastest one is this:
Code: Select all
:a
(Other Codes)
for /f %%i in ('SRT_MATH.bat 2 x 2') do set res=%%i
(Other Codes)
goto a
Just be sure to NOT include /A switch in SET command!
Antonio