1

I am having trouble using IF statements in a batch file. I have it set up with a set /p but when the code gets to the IF statement it closes. This file outlines what I mean:

@echo off  
set res1=word  
echo put in word.  
set /p ui=  
if %ui%==%res1% (goto oh)  
echo You put nothing in!  
pause  
:oh  
echo oh.  
pause

Running it and putting nothing in makes it crash, NOT echoing the text and pausing. What am I doing wrong?

1
  • 1
    Which Windows Version? On Windows 7 I do not see a Crash. Or what exactly do you mean with Crash? May 16, 2013 at 9:37

5 Answers 5

4

This is a common problem in a lot of scripting languages. The issue is that the variable names are expanded to their values before the line is executed. If the variable is left blank, the script will run the code:

if ==word (goto oh)

The two equal signs are not parsed as the equality operator because IF expects to find an item for comparison on both sides. Instead of (goto, an operator such as EQU is expected, and an error message will reflect that.

A similar problem will occur if the user enters a string containing spaces, e.g.:

if what I typed==word (goto oh)

This behaviour can be used for all sorts of dirty code. As an example, the condition will evaluate to true if the user enters 'word', but also if he enters '==word EQU ', in which case the line would be parsed as: if ==word EQU ==word (goto oh). Usually, we don't want to deal with those edge cases too extensively and can avoid the problem with blank variables by adding dummy characters, e.g.: if %a%dummy==%b%dummy. This way, the syntax will be valid even if one (or both) of the variables are empty. In batch files, a particularly useful dummy character is the "-symbol, as it also prevents spaces from being parsed as delimiters. The following code should be significantly more robust:

if "%ui%"=="%word%" (goto oh)
2
  • Oh right. The quotes have some use. I always thought they just looked better :)
    – Michael
    May 16, 2013 at 10:44
  • +1, Another improvement would be to explicitly clear the current value of ui before prompting for a value. To clear the value, use set "ui=". SET /P preserves the current value if the user does not enter anything.
    – dbenham
    May 16, 2013 at 13:45
3
If x%ui%x==x%res1%x

Does not have to be x. It's simply a trick to allow for the empty end var.

I usually use "%ui%"=="%res1%" because it looks more like a real language.

I learned this from ss64.com. Long time ago. Great site.

Edit: Square brackets is a safer option, as mentioned below.

2

Michael's solution to add double quotes could be problematic if the value you're testing has double quotes in it. A less likely to cause trouble way would be to use square brackets:

IF [%1]==[/?] ...

From Rob Van der Woude's site

If %1 itself may contain quotes you're in trouble: if %1 equals "/?" including the quotes, IF "%1"=="/?" ... evaluates to IF ""/?""=="/?" ... which will generate an error message.

0

Try forcing values to be updated at runtime, via Enable DelayedExpansion.
I wrote an IF command without spaces, and also batched with command in quotes; still got error ( was unexpected at this time. when called batch file from command line.
If launched from explorer, the batch file crashed.

Here's the crash code:

set miss=n
set /p miss=Any missing?
set miss=%miss:~0,1%

if /I %miss%==y (
Goto check2
) 

When I echo the value of miss before the IF statement, I get %miss:~0,1%

What appears to happen is at parse time, the value of variable isn't set by user. This happens at runtime, so to prevent errors, set at runtime.
TO fix this, I enabled delayed expansion, to force values to be updated at runtime, and works ok now.

set miss=n
set /p miss=Any missing?
set miss=!miss:~0,1!

if /I !miss!==y (
Goto check2
) 
0

I had some code like this:

if "%var%"=="word" (
goto top
)

Now I am not sure why, but to anyone having the same issue as me, I fixed by setting a variable called word to word, then checking %var% against that. Shown below.

set word = word

if "%var%"=="%word%" (
goto top
)

Notice I use a variable instead of just typing "word".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .