Computing.Net > Forums > Programming > GOTO was unexpected at this time

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

GOTO was unexpected at this time

Reply to Message Icon

Name: 9806245
Date: February 16, 2008 at 11:11:12 Pacific
OS: XP SP2
CPU/Ram: various
Comment:

I wrote a batch file, of which the following code is part:

:OutputFile
CLS
ECHO.
ECHO.
ECHO.
:OutputFileAfterError
ECHO What would you like to call the converted file?
SET OutputFile=
SET /P OutputFile=Type filename or Q to quit; ENTER sets filename to convertd.map:
IF /I '%OutputFile%'=='' SET OutputFile=convertd.map
IF /I '%OutputFile%'=='Q' GOTO End
IF /I NOT exist %CD%\%OutputFile% GOTO CommandKnown
CLS
ECHO.
ECHO File already exists, please choose again:
ECHO.
GOTO OutputFileAfterError

The problem is that when the %outputfile% doesn't exist, I get the following error:

GOTO not expected at this time.

I used the same check somewhere else in the batch file, and there it DOES work. Anyone got a clue?



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: February 16, 2008 at 14:19:09 Pacific
Reply:

Just a tip suggested by the official syntax of the IF statement:

As the /I switch has no meaning in the case of the [NOT] EXIST clause, remove it from the indicted line.

Batch scripts are very sensitive to syntax singularities.


0

Response Number 2
Name: klint
Date: February 18, 2008 at 06:33:23 Pacific
Reply:

IF /I NOT exist %CD%\%OutputFile%

As IVO said, no need for the /I. But I think the problem could be that %CD% when expanded contains spaces. Why do you have it there anyway? You don't need it: if you don't specify a directory, the default directory is %CD% anyway. Also, you should put %OutputFile% in quotes, in case there are spaces in there. So this becomes:

IF EXIST "%OutputFile%" GOTO CommandKnown

By the way, there is no CommandKnown label in your code snippet; I assume it exists but you haven't pasted it in.


0

Response Number 3
Name: tonysathre
Date: February 19, 2008 at 14:23:47 Pacific
Reply:

The label is there, he just forgot the colon.

Change CommandKnown to :CommandKnown


0

Response Number 4
Name: Mechanix2Go
Date: February 20, 2008 at 00:34:16 Pacific
Reply:

Oddly, goto eof won't work without the colon.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 5
Name: klint
Date: February 20, 2008 at 03:26:40 Pacific
Reply:

> Oddly, goto eof won't work without the colon.

That's right, and also if you use CALL :label you also need to have the colon. So for the sake of consistency I now always use the colon in front of all my goto targets.

(Except for the example I gave earlier, where I forgot...)


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: February 20, 2008 at 09:28:26 Pacific
Reply:

"if you use CALL :label you also need to have the colon"

That's because a CALL without the colon is not a label [a subroutine] it's another BAT.


=====================================
If at first you don't succeed, you're about average.

M2



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: GOTO was unexpected at this time

' was unexpected at this time error www.computing.net/answers/programming/-was-unexpected-at-this-time-error/18860.html

| was unexpected at this time. www.computing.net/answers/programming/-was-unexpected-at-this-time/16652.html

was unexpected at this time www.computing.net/answers/programming/was-unexpected-at-this-time/19145.html