jboy12
jboy12

Reputation: 3916

Simple batch file if() failure

I am working on a short batch file program that accepts input and does some simple math. It seems to work correctly for everything except the first echo. Here is the code:

set /p usercommand= "Input:"
if "%usercommand%" equ "done" (

set /p usertimeouthours= "Input Hours: "
echo (%usertimeouthours%)

set /p usertimeoutminutes= "Input Minutes: "
echo (%usertimeoutminutes%)

set /p usertimeoutseconds= "Input Seconds: "
echo (%usertimeoutseconds%)

set /a answer= %usertimeouthours%*3600+%usertimeoutminutes%*60+%usertimeoutseconds%
echo %answer%

goto end
) else (
echo finished
goto user
)
:end 

why does the first echo only output

(

my guess is something is wrong with my if statement, does anyone know how I should change it?

Thanks!

Upvotes: 2

Views: 1116

Answers (1)

dbenham
dbenham

Reputation: 130919

Problem 1 - Normal vs Delayed expansion of variables
Normal expansion using %var% occurs at parse time, and the entire IF statement is parsed all at once, including the contents within the parentheses. So something like echo (%usertimeouthours%) displays the value of USERTIMEOUTHOURS as it existed before the IF statement was executed (before SET /P set the value).

The fix is to enable delayed expansion at the top using setlocal enableDelayedExpansion and use !var! instead of %var%. Delayed expansion occurs at execution time instead of parse time.

Problem 2 - Unescaped special characters
There are a number of characters that have special meaning and must be either escaped with ^ or quoted if you want the character to be treated as a string literal.

One of the special characters is ). It will terminate any code block opened with ( unless it is escaped or quoted. You need to escape that character as ^) when you use it in your ECHO statements because those statements are within a parenthesized block of code.

Simplification of your SET /A statement
You do not need to expand variables when used in a SET /A computation. You can simply use the variable name without enclosing in percents or exclamations. This convenience only works with SET /A.

@echo off
setlocal enableDelayedExpansion
set /p usercommand= "Input:"
if "%usercommand%" equ "done" (

  set /p usertimeouthours= "Input Hours: "
  echo (!usertimeouthours!^)

  set /p usertimeoutminutes= "Input Minutes: "
  echo (!usertimeoutminutes!^)

  set /p usertimeoutseconds= "Input Seconds: "
  echo (!usertimeoutseconds!^)

  set /a answer= usertimeouthours*3600+usertimeoutminutes*60+usertimeoutseconds
  echo !answer!

  goto end
) else (
  echo finished
  goto user
)
:end

Upvotes: 1

Related Questions