有人可以对这些不同的产出做出解释

时间:2016-04-08 11:33:39

标签: windows batch-file command-line command command-prompt

请参阅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 个答案:

答案 0 :(得分:2)

我认为您的.bat文件与上面的SELECT tblTCOAppCodes.id, tblTCOAppCodes.tcoAppCode & " : " & tblTCOAppCodes.description AS Description FROM tblTCOAppCodes ORDER BY tblTCOAppCodes.tcoAppCode; (混淆了。还请阅读插入符号前缀:del checkos.txt

^

另请阅读有关set命令HERE

的信息

答案 1 :(得分: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 ----------------
GOTO :EOF

使用set "var=string"语法从解析器范围中删除"string",并确保该行上的任何尾随空格包含在分配的值中。

由于)中的programfiles(x86)现在在兔子的耳中,它是一个字符串的oart,有效地关闭了它的特殊含义(关闭else条款。)可能更传统使用原始语法的方法是使用插入符号来转义)^),就解析器而言,它会关闭字符的特殊含义,并将它们返回到普通字符。

??我不确定是否为32位操作系统建立了变量programfiles(x86)。如果是这样,

set "progfiledir=%programfiles%"
if defined programfiles(x86) set "progfiledir=%programfiles(x86)%"

似乎可以完成相同的结果......