views:

88

answers:

4
IF %processor_architecture% == AMD64 (SET querypath=hklm\software\x) ELSE (SET dsetquerypath=hklm\software\y)

FOR /F "tokens=* delims=\" %%G IN ('REG QUERY "%querypath%" 2^>NUL') DO ECHO %%G

Let me explain what im trying to accomplish out of this batch command. Basically there is a tool which gets intalled on hklm\software\x(on 32bit windows) and hklm\software\y(on 64 bit windows).

I need the exact path of the software from registry.Which could tell me whether the machine is 32 or 64 bit and take appropriate action. But right now every time I using this batch command it is always returning path as hklm\software\y.

I don't know WHY? That is what I need help to make this batch file right.

A: 

What about this:

ECHO processor_architecture="%processor_architecture%"
SET querypath=hklm\software\y
IF "%processor_architecture%" == "AMD64" SET querypath=hklm\software\x

Also note you have some typos on your ELSE part; isn't the problem there?

Rubens Farias
looks like %processor_architecture% is always returning x86 even on 64 bit machine.
alice7
well, if you could search registry twice, you can look for one path and, if don't find anything, search another one
Rubens Farias
A: 

I think you are falling victim to the phenomenon where the variables are expanded/evaluated as soon as they are read. There is a good discussion of that at Raymond Chen's blog.

Also search on "immediate expansion" and "delayed expansion".

shoover
Delayed expansion is not always the answer when environment variables are involved ...
Joey
+1  A: 

It works as expected if AMD64 is quoted:

set processor_architecture="AMD64"
IF %processor_architecture% == "AMD64" (SET querypath=hklm\software\x) ELSE > (SET  querypath=hklm\software\y)
echo querypath=%querypath%
wallyk
+2  A: 

EDIT: I think this may provide an explanation and solution to your problem, and in batch too :)

The spaces around the == may be causing your problem.

You're actually comparing the value of %processor_architecture%[space] to [space]AMD64

Try:

IF %processor_architecture%==AMD64...

If Command Extensions are enabled you can also do:

IF /I %processor_architecture% equ AMD64

(the /I switch makes the IF case insensitive)

Patrick Cuff
I just tried that, It is still giving me the same path for 64 bit machine
alice7
@alice7: Did you clean up the errors in your code snippet above, or are they typos? Do you know for sure the `%processor_architecture%` environment variable will contain "AMD64", or is it always "x86"? Johannes Rossel's suggestion to use WMI may be the way to go.
Patrick Cuff
Yes they are typos.I don't know, it is a environment variable so it says AMD64 for x64 bit machine but it says x86.
alice7