2

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!

jth41
  • 3,808
  • 9
  • 59
  • 109

1 Answers1

1

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
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • First of all fantastic Answer. Secondly, I understand when execution happens, but when does parsing happen? In other words, can you give me a short explanation of how excatly Batch files are run? You can respond in a comment or I can ask it as another question if you want rep for it ;) – jth41 Aug 09 '12 at 04:56
  • 1
    @John - You asked for it :-) Have a look at [jeb's answer](http://stackoverflow.com/a/4095133/1012053) to the question "How does the Windows Command Interpreter (CMD.EXE) parse scripts?" – dbenham Aug 09 '12 at 05:24
  • That link was exactly what I needed. Thanks so much. Also on a completely unrelated note. I just looked at your profile and you give really great answers.. but you almost never ask questions why is that? – jth41 Aug 09 '12 at 05:45