9

I have problem about IF ELSE in Command Batch script...

In Notepad:
Code:

:CHECKACCOUNT
if /I "%user%"=="insertusername" ( GOTO :ACCOUNT ) ELSE ( GOTO :CHECKPASSACCT )

:CHECKPASSACCT
if /I "%pass%"=="insertpassword" ( GOTO :ACCOUNT ) ELSE ( GOTO :COUNTER )

In COMMAND:
Code:

( was unexpected at this time.

FULL Script Code:

@echo off

::SETTINGS:::::::::::::::::::::::
set filetxt =userpass.txt
set log=logfile.log
set timer=900
::set default = true
::set user = 0
::set pass = 0
:::::::::::::::::::::::::::::::::


:STARTER
ECHO.>>%log%
ECHO ========START========>>%log%
SetLocal EnableDelayedExpansion
Set n=
Set _InputFile=%filetxt%
For /F "tokens=*" %%I IN (%_InputFile%) DO (
Set /a n+=1
Set acct!n!=%%I
)
set router_ip=%acct1%
set user=%acct2%
set pass=%acct3%
GOTO :CHECKFILE1


:CHECKFILE1
CLS
IF EXIST curl.exe ( GOTO :CHECKFILE2 ) else (
ECHO ERROR: curl.exe was not found.>>%log%
ECHO ERROR: curl.exe was not found.
ECHO.
ECHO.
GOTO :PAUSEEXIT
)

:CHECKFILE2
CLS
IF EXIST sleep.exe ( GOTO :CHECKACCOUNT ) else (
ECHO ERROR: sleep.exe was not found.>>%log%
ECHO ERROR: sleep.exe was not found.
ECHO.
ECHO.
GOTO :PAUSEEXIT
)

:CHECKACCOUNT
if /I "%user%"=="insertusername" GOTO ACCOUNT
GOTO CHECKPASSACCT

:CHECKPASSACCT
if /I "%pass%"=="insertpassword" GOTO ACCOUNT
GOTO COUNTER

:ACCOUNT
CLS
::if %default% = true ( GOTO :COUNTER ) ELSE (
ECHO To edit/change USERNAME and PASSWORD... Please type: OPTION
ECHO.
SET /P user="Please enter the username of your Router:"
IF /I %user%==OPTION ( Goto :EDITBAT )
CLS
ECHO To edit/change USERNAME and PASSWORD... Please type: OPTION
ECHO.
SET /P pass="Please enter the password of your Router:"
IF /I %pass%==OPTION ( Goto :EDITBAT )
CLS
set /a i = 1
GOTO :CHECKACCOUNT
::)

:EDITBAT
start /WAIT notepad %filetxt%
set router_ip=%acct1%
set user=%acct2%
set pass=%acct3%
GOTO :CHECKACCOUNT

:COUNTER
IF %i%==0 ( GOTO :RESETROUTER ) ELSE (
ECHO WAIT %i% seconds...
sleep 1
set /a i = i - 1
CLS
GOTO :COUNTER
)


:RESETROUTER
CLS
ECHO READY to RESET....
ECHO Preparing....
sleep 2

sleep 2
CLS
ECHO Processing....
sleep 5

sleep 2
CLS
ECHO Success....
sleep 5
set /a i = %timer%
CLS
GOTO :COUNTER

:PAUSEEXIT
PAUSE

:EXIT
ECHO.>>%log%
ECHO ========END OF LOG FILE========>>%log%
user453089
  • 719
  • 2
  • 13
  • 23

4 Answers4

18

Your error as seen comes from wrong formatting. Don't put it all on 1 line. Instead use this:

if /I "%user%"=="insertusername" (
     GOTO :ACCOUNT 
) ELSE (
     GOTO :CHECKPASSACCT 
)

The deeper, underlying reason is: goto :somewhere command needs to be terminated by a newline.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • If prefer @pipitas anser over @bta as it is simpler and does not side-step the question of `else` behavior. Both answers are good though. Just a side note, but I've noticed odd behavior calling `goto` within a loop where it branches to a line directly above an `if` statement. In those cases, oddly, placing an `echo nul>nul` or something similar between the destination and the `if` statement works like a charm. – kikuchiyo Feb 10 '12 at 17:09
  • 2
    A bit late, but the description is wrong. The syntax is correct, it's absolutly valid to use `if "1"=="1" (goto :label1) ELSE (goto :label2)`. `goto` doesn't need a newline – jeb Apr 26 '16 at 20:40
11

You can simplify this down to:

:CHECKACCOUNT
if /I "%user%"=="insertusername" GOTO ACCOUNT
GOTO CHECKPASSACCT

:CHECKPASSACCT
if /I "%pass%"=="insertpassword" GOTO ACCOUNT
GOTO COUNTER

The ELSE statements aren't needed. Since the IF block will jump somewhere else, placing the second GOTO on the next line or in an ELSE block should be equivalent.

Also, you need the leading colon when defining a GOTO target but not when referring to the target name inside the GOTO statement itself.

bta
  • 43,959
  • 6
  • 69
  • 99
  • 1
    Actually, if there's no code in between the two blocks then you can simplify it further and remove the `GOTO CHECKPASSACCT` line and the `:CHECKPASSACCT` line. – bta Sep 20 '10 at 19:33
  • When you say it still doesn't work, what error is it giving you and what line does the error occur on? It can't be the same error, since the offending `(` no longer exists. – bta Sep 20 '10 at 20:46
  • Also, within your `:COUNTER` block, you probably want to put `%i%` in double-quotes since you can enter that block before `i` is ever set. – bta Sep 20 '10 at 21:01
  • Adding quotation marks will resolve the syntax error in that line, but your logic is still broken. The variable `i` needs to be initialized to some valid starting value so that you will never hit the `:COUNTER` block before you explicitly set it. Editing the batch file and re-testing in the same shell window can cause variables like `i` to be re-used between runs, so try adding commands like `set i=` at the top of the batch file to clear out any old data before the script executes (this can hide uninitialized variable problems like this one). – bta Sep 20 '10 at 21:12
  • aws... its working now.. look like i missing "set i=" Thanks bta for helping me... :) – user453089 Sep 21 '10 at 00:14
3

The problem has nothing to do with the shown code!

The problem is at

:COUNTER
IF %i%==0 ( GOTO :RESETROUTER ) ELSE (

As the variable i isn't defined, the line will be expanded to

IF ==0 ( GOTO :RESETROUTER ) ELSE (

That is an invalid expression.

To find such simple syntax fails, you should enable debugging with ECHO ON.

jeb
  • 78,592
  • 17
  • 171
  • 225
0

You don't need to use the parentheses. You're basically telling the system "if %user% is insertusername (" and it's asking "what is the '(' supposed to do?"

  • WRONG, the first parenthesis block have to be used here to build a command block. Without the parenthesis the ELSE can't be parsed (it will be part of the `goto` command) and the `IF` failed for `false` conditions – jeb Apr 26 '16 at 20:40