Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
Which Windows Version? On Windows 7 I do not see a Crash. Or what exactly do you mean with Crash? –  Werner Henze May 16 '13 at 9:37
add comment

3 Answers

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)
share|improve this answer
    
Oh right. The quotes have some use. I always thought they just looked better :) –  Michael May 16 '13 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 '13 at 13:45
add comment

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.

share|improve this answer
add comment
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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.