Solved More on IF

May 31, 2011 at 17:39:17
Specs: Windows XP
It appears that you cannot do a "Set /P..." command within an IF statement block.
For example:

if %var%==1 (
Set /P value=Enter new value:
...
)

I get error ": was unexpected at this time". If I comment out the Set line, it works fine.

And interestingly the comment works as long as I use the "rem" command. If I use what I
normally use, which is two colons "::", I get error ") was unexpected at this time". But for
comments outside of a block, the colons work great.

Even if I leave the colon off of the end of the Set statement, it still does funny things. What is
the rule about what can be put in a code block?


See More: More on IF

Report •

#1
May 31, 2011 at 20:47:43
It appears that you cannot do a "Set /P..." command within an IF statement block. (?????)

@echo off
cls
setlocal

set p=1

if %p% equ 1 (
   set /p dumbe=enter data:
)
echo %dumbe%

The script you show would probably work ok if you used:

if "%var%"=="1"
so that it becomes a string comparison.

What is the rule about what can be put in a code block?

Enter IF /? at the command prompt.


Please come back & tell us if your problem is resolved.


Report •

#2
June 1, 2011 at 09:57:41
Adding quotes as you suggested made no difference. And the IF /? help
doesn't have anything different than what Windows Help provides.

But, I did figure out what was happening. In my prompt command I put the
valid input range in parenthesis.

Set /P value=Enter New Value (0-10):

Well, the closing parenthesis was interpreted as ending the IF statement
clause.

If %var%==1 (
Set /P value=Enter New Value (0-10):
)

That Mystery solved.


Report •

#3
June 1, 2011 at 12:38:30
✔ Best Answer
You can escape if you want to keep the parens:

If %var%==1 (
     Set /P value=Enter New Value (0-10^):
)


Report •
Related Solutions


Ask Question