zodkc
zodkc

Reputation: 43

IF Statement isn't working. Batch Files

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?

Upvotes: 2

Views: 2620

Answers (3)

user3076409
user3076409

Reputation: 23

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.

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881113

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> _

Upvotes: 1

Mathieu
Mathieu

Reputation: 4520

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.

Upvotes: 0

Related Questions