2

I am a beginner at batch files. So I was making a basic batch file and when I tested it out, it said The syntax of the command is incorrect. But I am quite sure that it is correct. I knew it was in the IF parts of my coding, as I put several wait scripts(using VBS). I don't use REM just yet so I don't know what the problem is. What I wrote is something like this.

@echo off

b:
set a=1

IF %a%==1 goto :a

:a

echo Test

pause

goto :b

I also have quite alot of other IF %variable%==number statements in the coding and they all work fine. What is going on?

0

3 Answers 3

1

For a start, your label b has the colon on the wrong side, turning it into a command that tries to set the default drive to your second floppy :-)

Secondly, although not strictly necessary, I prefer labels to be on their own line:

    @echo off
:b
    set a=1
    IF %a%==1 goto :a
:a
    echo Test
    pause
    goto :b

Running this results in:

C:\Users\Pax\Documents> testprog
Test
Press any key to continue . . . <ENTER>
Test
Press any key to continue . . . <ENTER>
Test
Press any key to continue . . . <ENTER>
Test
Press any key to continue . . . <CTRL-C>
Terminate batch job (Y/N)? y

C:\Users\Pax\Documents> _
3
  • Seems like it was a label issue. Though I do have another problem which I have a theory of. If you use the IF statement without the variable set to anything, will it not work? Thanks again.
    – zodkc
    Commented Apr 19, 2012 at 6:30
  • Resolved the issue, the theory I had was correct. Sorry for the bother. :)
    – zodkc
    Commented Apr 19, 2012 at 6:32
  • Variables set to nothing work okay. You only have to try echo [%xyzz%] to see that in action. You'll get the uninterpreted [%xyzzy%] if it's not set. If it's set to 1 (for example), you'll get [1]. In any case, your variable was set in the code given.
    – paxdiablo
    Commented Apr 19, 2012 at 6:41
0
IF %a%==1 goto :a

Should be

IF %a%==1 goto a

For further help, here's where I found the solution: http://www.computerhope.com/batch.htm It has all the info you need.

1
  • 3
    This isn't correct, goto with the label containing the colon works just fine.
    – paxdiablo
    Commented Apr 19, 2012 at 3:22
0

Like paxdiablo said it may not be necessary but it can help to give tags their own line and there can be other little things that may not be necessary but can help you be a bit organized which will help you in the long run one way it can be fixed up is like so for a example.

@echo off
:b
set a=1
IF %a%==1 goto a
:a
echo Test
pause
goto b

Also the IF %a%==1 goto a can be changed to IF %a% EQU 1 goto a just like the goto can be written as goto :a or goto a . a Title can be added with Title title of window etc..

Happy exploring.

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