views:

293

answers:

2

I am writing a simple batch file and i get this "The syntax of the command is incorrect" error for the if statement.The batch file is as below:

@echo off

set ARCH=%PROCESSOR_ARCHITECTURE%

if %ARCH% == 'x86'
(
)

I also tried

if %ARCH% EQU 'x86'

What is wrong with the if statement?

+4  A: 

try

@echo off
set ARCH=%PROCESSOR_ARCHITECTURE%
if %ARCH% == x86 ( echo ok )
rem or %ARCH% EQU x86

and while you are at it, why not learn vbscript as well, here's the equivalent

Set WshShell = WScript.CreateObject("WScript.Shell")
Set Wsharch = WshShell.Environment("SYSTEM")
arch=Wsharch.Item("PROCESSOR_ARCHITECTURE")
If arch = "x86" Then
    WScript.Echo "PROCESSOR_ARCHITECTURE is x86"
End If
' if Instr(arch,"86") > 0 then
ghostdog74
That worked. Thanks.Actually i want to check if the architecture contains string 64.Something like:if %ARCH% == '*64*' ( echo ok )How can i do that?
Sam
i am not a batch expert, so i hope someone can help you with that. however, I do know that vbscript does what you want more easily, like searching for strings, regular expressions and better data structures like arrays, dictionaries etc. So if you are interested, learn how to use vbscript instead.
ghostdog74
Already done :-). Still, arrays and dictionaries come pretty naturally in batch files with environment variables. They're sorta a catch-all for those things ;-)
Joey
+3  A: 

ETA:

Actually i want to check if the architecture contains string 64. Something like:

if %ARCH% == '*64*' ( echo ok )

How can i do that?

For this you can employ the help of a nice little tool called findstr which searches for patterns in files or command output. Just pipe the value of the variable into findstr:

echo %ARCH% | findstr 64

which will find any line in the output that contains 64 somewhere. In this case, there is only one line but that's fine.

However, this also causes the line to be output on screen, so change it into the following:

echo %ARCH% | findstr 64 > nul 2>&1

which will redirect any output (including error messages) into nothingness.

Now, findstr sets a special value (the exit code, or error level) depending on whether the string was found or not. You can use that to test whether 64 in fact appeared in the output:

echo %ARCH% | findstr 64 > nul 2>&1
if errorlevel 1 (
    echo 64 did NOT appear (error level greater 0 usually means failure)
) else (
    echo 64 DID appear
)

To your original question:

The opening parenthesis must occur on the same line as the if statement itself:

@echo off 

set ARCH=%PROCESSOR_ARCHITECTURE% 

if %ARCH% == x86 ( 
    rem ...
) 

does work.

Remember that cmd's parser is line-based; therefore caution is advised with anything that needs to span multiple lines. Another option would be to escape the line break:

@echo off 

set ARCH=%PROCESSOR_ARCHITECTURE% 

if %ARCH% == x86 ^
( 
    rem ...
)

The ^ is the escape character in cmd and in this use it's effectively a line-continuation operator so the parser accepts the opening parenthesis on the next line as part of the current statement (which usually would be terminated by a line break).

And yes, the use of single quotes for the literal string value in the comparison will get you into other problems as Carlos Gutiérrez noted, since essentially everything in cmd is a string literal. So you would be comparing with the literal string

'x86'

instead of

x86

what is actually contained in the environment variable.

Joey