English 中文(简体)
Need help in finding the root cause in this wrong batch command
原标题:
IF %processor_architecture% == AMD64 (SET querypath=hklmsoftwarex) ELSE (SET dsetquerypath=hklmsoftwarey)

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 hklmsoftwarex(on 32bit windows) and hklmsoftwarey(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 hklmsoftwarey.

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

问题回答

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)

It works as expected if AMD64 is quoted:

set processor_architecture="AMD64"
IF %processor_architecture% == "AMD64" (SET querypath=hklmsoftwarex) ELSE > (SET  querypath=hklmsoftwarey)
echo querypath=%querypath%

What about this:

ECHO processor_architecture="%processor_architecture%"
SET querypath=hklmsoftwarey
IF "%processor_architecture%" == "AMD64" SET querypath=hklmsoftwarex

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

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".





相关问题
complex batch replacement linux

I m trying to do some batch replacement for/with a fairly complex pattern So far I find the pattern as: find ( -name *.php -o -name *.html ) -exec grep -i -n hello {} + The string I want ...

How to split a string by spaces in a Windows batch file?

Suppose I have a string "AAA BBB CCC DDD EEE FFF". How can I split the string and retrieve the nth substring, in a batch file? The equivalent in C# would be "AAA BBB CCC DDD EEE FFF".Split()[n]

How to check which Operating System?

How can I check OS version in a batch file or through a vbs in an Windows 2k/2k3 environment ? You know ... Something like ... : "If winver Win2k then ... or if winver Win2k3 then ....

热门标签