I would like to perform numeric calculations in DOS. I code simplified the following:
set /p func=Enter function=
set /p a=Enter a=
set /p b=Enter b=
set /p c=Enter c=
If [func]==[x] (
set /a res=^(%a% - %b%^) / %c%
Echo res=%res%)
Echo End of calcs
The problem is that the script doees NOT echo the result.
Whats wrong?
I guess the brackets are the culprit. I masked them with carets but it didn't help.
So how else can I perform a function calc WITH brackets INSIDE an "if"/"else" branch enclosed itself by brackets?
Further question: How can I check if a variable value is numeric?
Further question: How can I check if a variable value is numeric and greater zero?
Peter
Howto calc math expressions with brackets in "if"?
Moderator: DosItHelp
Re: Howto calc math expressions with brackets in "if"?
Your IF is testing if func is equal go X. It should be testing the value of %func% is equal to X.
Re: Howto calc math expressions with brackets in "if"?
Squashman wrote:Your IF is testing if func is equal go X. It should be testing the value of %func% is equal to X.
Yes, you are rigth. Its a typo. Sorry.
But the core problems remains even if I code
...
If [%func%]==[x] (
...
Re: Howto calc math expressions with brackets in "if"?
EDIT: I wasn't clear - this is two answers to the questions asked.
Double quotes protect the brackets.
Double quotes protect the brackets.
Code: Select all
for /f "delims=0123456789" %%a in ("%variable%") do echo not numeric.
Re: Howto calc math expressions with brackets in "if"?
Your RES variable is not echoing to the screen because it is inside a code block and you are not using delayed expansion. Move that echo outside of the parentheses or use delayed expansion.
Code: Select all
@echo off
setlocal enabledelayedexpansion
set /p func=Enter function=
set /p a=Enter a=
set /p b=Enter b=
set /p c=Enter c=
If [%func%]==[x] (
set /a "res=(%a% - %b%) / %c%"
Echo res=!res!
)
Echo End of calcs
pause