12

I'm using the batch script below and get an error

( was unexpected at this time.

I know that the problem is in the first line but I don't understand what is wrong. Any ideas ?

script:

IF [%1]==[] (
    :LOOP1
    SET /P isDefault=Value Missing, do you want to use default values [1,1,10,Local      Area Connection 2]?[y/n]
    IF %isDefault%==y (
        SET from=1
        SET step=1
        SET to=10
        SET lan="Local Area Connection 2"
        GOTO :USERLOOP
    )
    IF %isDefault%==n GOTO :END
    GOTO :LOOP1 
)
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
choppy
  • 739
  • 1
  • 12
  • 22

3 Answers3

18

Actually, the problem is not on the first line.

The problem is that cmd does variable substitution immediately when it parses the IF statement, including its body. Therefore the line:

IF %isDefault%==y (

is problematic because isDefault isn't set when the outer IF statement is parsed, so it becomes:

IF ==y (

and hence you get the error about ( being unexpected. You can get around this by enabling the command extension (SETLOCAL ENABLEDELAYEDEXPANSION) for delayed environment variable expansion (see set /? for details). You also can rewrite your script:

@ECHO OFF
IF NOT "%1"=="" GOTO :EOF

:LOOP1
SET /P isDefault=Value Missing, do you want to use default values [1,1,10,Local Area Connection 2]?[y/n]
IF "%isDefault%"=="y" (
    SET from=1
    SET step=1
    SET to=10
    SET lan="Local Area Connection 2"
    GOTO :USERLOOP
)
IF "%isDefault%"=="n" GOTO :EOF
GOTO :LOOP1

(I made some other changes, such as using the built-in :EOF label instead of :END.)

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • You dramatically changed the intended behavior with your first IF statement. I can't be sure, but it should probably read `IF NOT "%~1"=="" GOTO :USERLOOP`. You are assuming there is no code after the `:END` label - perhaps there is cleanup code at the end that must be performed, so `GOTO :EOF` may not be correct. – dbenham Mar 21 '12 at 10:21
  • @dbenham: Argh again. I did mean `IF NOT` on the first line. That's what I get for answering in a rush. Yes, I made some assumptions about `:END`; hard to say without seeing more of the script. – jamesdlin Mar 21 '12 at 10:35
  • Still not correct - I think it should GOTO :USERLOOP. If parameters are provided, then the batch goes directly to the functioning part of the code (:USERLOOP). Otherwise it enters the default parameter processing before possibly going to :USERLOOP. – dbenham Mar 21 '12 at 11:29
4

As jamesdlin said, it's a problem with empty variables and also with delayedExpansion.
Then the solution is simple by replacing %isDefault% with !isDefault!, this works even if isDefault is empty.

setlocal EnableDelayedExpansion
IF [%1]==[] (
    :LOOP1
    SET /P isDefault=Value Missing, do you want to use default values [1,1,10,Local      Area Connection 2]?[y/n]
    IF !isDefault!==y (
        SET from=1
        SET step=1
        SET to=10
        SET lan="Local Area Connection 2"
        GOTO :USERLOOP
    )
    IF !isDefault!==n GOTO :END
    GOTO :LOOP1 
)
jeb
  • 78,592
  • 17
  • 171
  • 225
  • Jeb - how naughty of you preserving that goto label within an IF block :) I realize that the code will work fine as written. But choppy will get a rude surprise if he/she thinks this is ok, and then does the same when there is an ELSE clause. @choppy - read this question and my answer to see what I am referring to: [(Windows batch) Goto within if block behaves very strangely](http://stackoverflow.com/questions/8481558/windows-batch-goto-within-if-block-behaves-very-strangely) – dbenham Mar 21 '12 at 10:40
2

I had a very similar problem and code construct that was causing me a lot of pain to resolve. My error message was ". was unexpected at this time"...

It took me a couple long days to figure out another similar consideration due to this problem... Please take a look a the following problem and subsequent solution here: ". was unexpected at this time" generated from batch script line 'if exist [file] (...

The solution was simply the treatment of '(' and ')' on ECHO lines inside an IF statement block.

The point is, do consider treatment of special characters as a possible source of a problem when troubleshooting IF (and possibly FOR) statements.

HTH someone...

sw.smayer97
  • 365
  • 3
  • 9