Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 OutputFileAfterErrorThe 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?

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.

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.

Oddly, goto eof won't work without the colon.
=====================================
If at first you don't succeed, you're about average.M2

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

"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

![]() |
![]() |
![]() |

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