Capturing Math batch's output to a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Capturing Math batch's output to a variable

#1 Post by Ranguna173 » 29 Apr 2013 13:46

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. :)

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Capturing Math batch's output to a variable

#2 Post by Endoro » 29 Apr 2013 14:01

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.

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Capturing Math batch's output to a variable

#3 Post by Ranguna173 » 29 Apr 2013 14:57

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.bat
Ex:.

Code: Select all

SRT_MATH.bat 2 X 2

Output in the CMD window:

Code: Select all

C:\> SRT_MATH.bat 2 X 2
4

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

Re: Capturing Math batch's output to a variable

#4 Post by miskox » 29 Apr 2013 15:12

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

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Capturing Math batch's output to a variable

#5 Post by Ranguna173 » 29 Apr 2013 15:15

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

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Capturing Math batch's output to a variable

#6 Post by Ranguna173 » 29 Apr 2013 15:24

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 :D

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Capturing Math batch's output to a variable

#7 Post by Endoro » 29 Apr 2013 15:34

Code: Select all

@echo off & setlocal
for /f %%i in ('SRT_MATH.bat 2 x 2') do set /a res=%%i
echo %res%

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Capturing Math batch's output to a variable

#8 Post by Ranguna173 » 29 Apr 2013 15:50

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.

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

Re: Capturing Math batch's output to a variable

#9 Post by foxidrive » 29 Apr 2013 17:07

This should work as well, without the start.

Code: Select all

>file.txt call SRT_MATH.bat 2 x 2

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Capturing Math batch's output to a variable

#10 Post by Ranguna173 » 30 Apr 2013 11:46

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.

carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

Re: Capturing Math batch's output to a variable

#11 Post by carlsomo » 13 May 2013 00:09

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/#math

Code: 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%

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

Re: Capturing Math batch's output to a variable

#12 Post by Aacini » 13 May 2013 09:08

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

Post Reply