1

Recently I've encountered yet another problem in my Batch file. Where I want something to be performed, I'm getting the following error:

= was unexpected at this time.

I've had this error before, however it's always due to a small mistake which I fixed up. This time I can't identify it.

choice /c 12b /n
if %errorlevel%==1 (
if not exist C:\ntbobdings\1.txt (
set bding=Variable
set bdingno=1
goto IfNot
)

What is wrong? It's a small area, yet the if %errorlevel%==1 (blah) seems fine.

dan1st
  • 12,568
  • 8
  • 34
  • 67
user2960215
  • 43
  • 1
  • 4

2 Answers2

6

The problem is if the variable your using has no value it returns - well nothing. Thus you're inputing:

if ==value Echo Test.

To avoid this surrounding the variable in "'s even if it has no value you input:

if ""=="value" Echo Test.

In other words just do:

choice /c 12b /n
if "%errorlevel%"=="1" (
    if not exist C:\ntbobdings\1.txt (
    set bding=Variable
    set %bdingno%=1
    goto IfNot
))

And that should work fine and help you understand whats wrong.

Mona.

Monacraft
  • 6,510
  • 2
  • 17
  • 29
  • I tried coating my 'if' statement variables in quotation marks, however I still get the same error. Thank you very much for your input, however. I might upload some more of the code to help anyone answering. – user2960215 Nov 07 '13 at 00:03
  • 1
    Using `set %bdingno%=1` will create a variable named whatever the output of %bdingno% is. He hasn't said that this is what he wants. It should be `set bdingno=1`. – unclemeat Nov 07 '13 at 00:33
  • @Daemon - that is incorrect; if, for example, `%bdingo%` contained the word `'test'`, that line would create a variable called `%"test"%` containing `"1"`. It works in the if statement as it is just a comparison - it is a way of handling empty variables. – unclemeat Nov 07 '13 at 00:38
  • 2
    @aruuu Apologies, you are, of course, correct. That's what I get for skimming the answer. I'll delete my comment to prevent future confusion. – Taylor Hx Nov 07 '13 at 00:53
  • Um, no - `%errorlevel%` ALWAYS contains a value, although the principle is sound. The problem may be the missing close-parenthesis which would include the unposted remaining code into the `if %errorlevel%` statement, OR it could be that the variable `errorlevel` has been assigned a value of space(s) or comma or semicolon which overrides the magic values assigned by CMD (provided the error isn;t actully in the unposted following code) – Magoo Nov 07 '13 at 00:55
  • Now I know why people always wrap the variable in some other characters – phuclv Jan 08 '16 at 10:46
2

Try this:

@ECHO OFF &SETLOCAL
choice /c 12b /n
if %errorlevel%==1 (
    if not exist C:\ntbobdings\1.txt (
        set bding=Variable
        set bdingno=1
        goto IfNot
    )
)
goto:eof
:ifnot
echo Hello world!
captcha
  • 3,756
  • 12
  • 21