IF:Command Syntax Incorrect; Tell me what's wrong, please.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Razor
Posts: 5
Joined: 03 Dec 2012 03:20

IF:Command Syntax Incorrect; Tell me what's wrong, please.

#1 Post by Razor » 03 Dec 2012 03:46

Hi,

Can someone tell me what's wrong with the following:

Code: Select all

@echo off
TITLE JRE Test
COLOR 0A
cls
:32-bitjre7
IF EXIST %windir%\Java\jre7
goto jre7
) ELSE (
echo You are using another version of JRE or it's not installed.
ping localhost -n 2 >nul
echo.
echo Moving on to JRE6 Test.
ping localhost -n 2 >nul
COLOR 09
cls
:32-bitjre6
IF EXIST %windir%\Java\jre6
goto jre6
) ELSE (
echo JRE is not installed.
goto nojre
:jre7
COLOR 0A
cls
echo Java Runtime Environment 7 is installed.
echo Press any key to exit.
pause >nul
exit
:jre6
COLOR 09
cls
echo Java Runtime Environment 6 is installed.
echo Press any key to exit.
pause >nul
exit
:nojre
COLOR 0C
cls
echo Java Runtime Environment is not installed.
echo Press any key to exit.
pause >nul
exit


Thanks :D

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: IF:Command Syntax Incorrect; Tell me what's wrong, pleas

#2 Post by Ed Dyreen » 03 Dec 2012 05:28

The syntax of the if command states that everything is on one line

Code: Select all

if 1==1 echo.yes
if else is used the first option within a block

Code: Select all

if 1==1 (echo.yes) else echo.no
Or all within block, this is required for multiline statements

Code: Select all

if 1==1 (echo.yes) else (echo.no)
if 1==1 (
       echo.yes
) else (
       echo.no
)
This is your problem here. In java the compiler would complain about this.
java wrote:if ( 1 == 1 ) {
multiline statements
}

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: IF:Command Syntax Incorrect; Tell me what's wrong, pleas

#3 Post by foxidrive » 03 Dec 2012 06:04

Razor wrote:Hi,

Can someone tell me what's wrong with the following:

Code: Select all


IF EXIST %windir%\Java\jre7

IF EXIST %windir%\Java\jre6




Java is not likely to be installed in %windir%\

Razor
Posts: 5
Joined: 03 Dec 2012 03:20

Re: IF:Command Syntax Incorrect; Tell me what's wrong, pleas

#4 Post by Razor » 03 Dec 2012 06:36

@Ed Dyreen I don't quite follow you. :oops: Can you correct where I'm wrong?

@Foxidrive I know, I put %windrive% there because then that would say it doesn't exist.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: IF:Command Syntax Incorrect; Tell me what's wrong, pleas

#5 Post by Squashman » 03 Dec 2012 07:03

Razor wrote:@Ed Dyreen I don't quite follow you. :oops: Can you correct where I'm wrong?


ED showed you some pretty basic examples of the correct syntax for an IF ELSE command. At least make an attempt.

Razor
Posts: 5
Joined: 03 Dec 2012 03:20

Re: IF:Command Syntax Incorrect; Tell me what's wrong, pleas

#6 Post by Razor » 03 Dec 2012 07:28

The syntax of this... is still wrong

Code: Select all

IF EXIST %windir%\Java\jre7 (
goto jre7
) ELSE (
goto 32-bitjre6


Why? o_o

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: IF:Command Syntax Incorrect; Tell me what's wrong, pleas

#7 Post by Squashman » 03 Dec 2012 07:55

You are missing the closing parenthesis.

Razor
Posts: 5
Joined: 03 Dec 2012 03:20

Re: IF:Command Syntax Incorrect; Tell me what's wrong, pleas

#8 Post by Razor » 03 Dec 2012 08:05

And now... I come to another problem :?

Code: Select all

@echo off
TITLE JRE Test
COLOR 0A
cls
:32-bitjre7
IF EXIST %windir%\Java\jre7 (
goto jre7
) ELSE (
goto 32-bitjre6
)
:32-bitjre6
COLOR 09
cls
IF EXIST %programfiles%\Java\jre6 (
goto jre6
) ELSE (
goto nojre
)
:jre7
COLOR 0A
cls
echo Java Runtime Environment 7 is installed.
echo Press any key to exit.
pause >nul
exit
:jre6
COLOR 09
cls
echo Java Runtime Environment 6 is installed.
echo Press any key to exit.
pause >nul
exit
:nojre
COLOR 0C
cls
echo Java Runtime Environment is not installed.
echo Press any key to exit.
pause >nul
exit


According to this, it's supposed to go to 32-bitjre6 because %windir%\Java\jre7 does not exist. Once it's in 32-bitjre6 it is supposed to check if %programfiles%\Java\jre6 exists. It says JRE6 is installed, even if jre6 doesn't exist.

As you can see, jre6 doesn't exist. Image

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: IF:Command Syntax Incorrect; Tell me what's wrong, pleas

#9 Post by Ed Dyreen » 03 Dec 2012 08:21

Razor wrote:According to this, it's supposed to go to 32-bitjre6 because %windir%\Java\jre7 does not exist. Once it's in 32-bitjre6 it is supposed to check if %programfiles%\Java\jre6 exists. It says JRE6 is installed, even if jre6 doesn't exist.
This indicates the if statement failed to run, the error was ignored and cmd continued executing the next line.
The if statement failed because of the space in "program files".

This can be solved by using delayed expansion

Code: Select all

setlocal enableDelayedExpansion
IF EXIST !programfiles!\Java\jre6 (echo.EXIST %programfiles%\Java\jre6)else echo.notEXIST %programfiles%\Java\jre6
or placing the string within quotes.

Code: Select all

IF EXIST "%programfiles%\Java\jre6" (echo.EXIST %programfiles%\Java\jre6)else echo.notEXIST %programfiles%\Java\jre6

Razor
Posts: 5
Joined: 03 Dec 2012 03:20

Re: IF:Command Syntax Incorrect; Tell me what's wrong, pleas

#10 Post by Razor » 03 Dec 2012 08:48

Finally! :mrgreen: Thanks alot guys.

Post Reply