【发布时间】:2016-07-29 15:43:38
【问题描述】:
请查看 2 个示例代码:
@ECHO off
Set RegQry=HKLM\Hardware\Description\System\CentralProcessor\0
REG.exe Query %RegQry% > checkOS.txt
Find /i "x86" < CheckOS.txt > StringCheck.txt
If %ERRORLEVEL% == 0 (
Echo "This is 32 Bit Operating system"
set ProgFileDir=%ProgramFiles%
) ELSE (
Echo "This is 64 Bit Operating System"
set ProgFileDir=%ProgramFiles(x86)%
)
del checkos.txt
del stringcheck.txt
echo ---------------
echo "%ProgFileDir%"
echo ----------------
pause
这给了我一个错误的输出:
"This is 64 Bit Operating System"
---------------
"C:\Program Files (x86" <==Notice how the closing brace is trimmed
----------------
Press any key to continue . . .
如果我在括号外更改语句set ProgFileDir=%ProgramFiles(x86)% 的位置:
@ECHO off
Set RegQry=HKLM\Hardware\Description\System\CentralProcessor\0
REG.exe Query %RegQry% > checkOS.txt
Find /i "x86" < CheckOS.txt > StringCheck.txt
If %ERRORLEVEL% == 0 (
Echo "This is 32 Bit Operating system"
set ProgFileDir=%ProgramFiles%
) ELSE (
Echo "This is 64 Bit Operating System"
)
del checkos.txt
del stringcheck.txt
set ProgFileDir=%ProgramFiles(x86)%
echo ---------------
echo "%ProgFileDir%"
echo ----------------
pause
我得到了正确的结果:
"This is 64 Bit Operating System"
---------------
"C:\Program Files (x86)" <== with the closing brace
----------------
Press any key to continue . . .
我在这里做错了什么?我在 Windows 7 64 位操作系统上运行这个批处理文件。
【问题讨论】:
-
除了您的问题,您可以通过检测系统变量来简化代码,而无需创建 2 个临时文件。 http://ss64.com/nt/syntax-64bit.html
标签: windows batch-file command-line command command-prompt