0

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
    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
3

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)
| 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
2
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.

| improve this answer | |
1

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.

| improve this answer | |
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
) 
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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