批处理脚本不是 运行

Batch script not running

你能帮帮我吗?

这段脚本不是 运行,我不明白为什么。

@echo off
ver | findstr /i "5\.1\."
if %ERRORLEVEL% EQU 0 (
    set os_ver="xp"
)

ver | findstr /i "6\.1\." > nul
if %ERRORLEVEL% EQU 0 (
    set os_ver="7"
)

if %os_ver% == "xp" (
    set os_bits="32"
)

if %os_ver% == "7" (
    if %PROCESSOR_ARCHITECTURE% == "x86" (
        set os_bits="32"
    ) else (
        set os_bits="64"
    )
    echo %os_bits%
)
pause

尽管 "ECHO IS DEACTIVATED" 或 "ECHO IS ACTIVATED"

但它没有回显任何内容

更新: 我发布了整个代码,因为人们说它有效

更新2: 我在 Windows 7 64 位

试试这个:

@echo off
setlocal
if "%PROCESSOR_ARCHITECTURE%" equ "x86" (
    set os_bits="32"
) else (
    set os_bits="64"
)
echo %os_bits%

批处理脚本似乎工作正常。如果您的机器不是 Windows 7,它不会回显。

尝试以下操作:

@echo off

set os_ver="unknown!"
set os_bits="unknown!"

ver | findstr "5\.1" > nul
if %ERRORLEVEL% EQU 0 (
    set os_ver="xp"
)

ver | findstr "6\.1" > nul
if %ERRORLEVEL% EQU 0 (
    set os_ver="7"
)

if %os_ver% == "xp" (
    set os_bits="32"
)

if %os_ver% == "7" (
    if %PROCESSOR_ARCHITECTURE% == "x86" (
        set os_bits="32"
    ) else (
        set os_bits="64"
    )
)

echo os_ver = %os_ver%
echo os_bits = %os_bits%

pause

更新:评论者 eryksun 提供了正确的原因,说明为什么这段代码有效,而操作人员却没有,即使他在 Windows7。 (不错)

In the OP's version echo %os_bits% is executed within the same command that sets os_bits. At the time the command is parsed and dispatched os_bits doesn't exist (unless it was already set), so it's just executing echo, which prints whether echo is on or off. – eryksun