将值重新分配给现有批处理变量的问题

Problem to reassign a value to an existing batch variable

我正在为批处理文件中的这个小烦恼而苦苦挣扎:

这是开始的代码:

SET CAMFORLARGEPARTS=YES
IF "%CAMFORLARGEPARTS%" EQU "NO" (SET REGISTRYFILEPARTSIZES=Normal_Parts) ELSE (SET REGISTRYFILEPARTSIZES=Large_Parts)
SET PROMPTFORPARTSIZE=YES

在路上,我们也有这个,在同一个 .cmd 文件中

我正在尝试为 %CAMFORLARGEPARTS% 重新分配一个不同的值,但没有成功

:ASKABOUTPARTSIZE
ECHO OFF
IF "%PROMPTFORPARTSIZE%" NEQ "YES" GOTO SKIPTHIS

CHOICE /C YN /N /T 10 /D Y /M "USE LARGE PARTS? -> PRESS 'Y' FOR YES | 'N' FOR NO"
ECHO ON
ECHO.
ECHO THE ERRORLEVEL VALUE RETURNED BY CHOICE.EXE IS %ERRORLEVEL%

IF ERRORLEVEL 1 SET CAMFORLARGEPARTS=YES
IF ERRORLEVEL 2 SET CAMFORLARGEPARTS=NO
ECHO.
ECHO THE VALUE SET FOR VARIABLE CAMFORLARGEPARTS IS %CAMFORLARGEPARTS%

PAUSE

EXIT

:SKIPTHIS
...

这些是我得到的结果。我不明白为什么甚至根据用户接收和传递正确的错误级别 s 输入,我总是将 %CAMFORLARGEPARTS% 设置为 YES。

我做错了什么?

万分感谢!

尝试:

IF %ERRORLEVEL% EQU 1 SET CAMFORLARGEPARTS=YES
IF %ERRORLEVEL% EQU 2 SET CAMFORLARGEPARTS=NO

IF ERRORLEVEL 1 检查 ERRORLEVEL 是否等于 1 或大于 1,这是有问题的,因为如果 ERRORLEVEL2 那么它将也大于一。

根据我评论的建议,这里有一个示例,不会对您的结构进行太多更改。

:ASKABOUTPARTSIZE
@ECHO OFF
IF /I NOT "%PROMPTFORPARTSIZE%" == "YES" GOTO SKIPTHIS

%SYSTEMROOT%\SYSTEM32\CHOICE.EXE /C YN /N /T 10 /D Y /M "USE LARGE PARTS? -> PRESS 'Y' FOR YES | 'N' FOR NO"
SET "CHOICECODE=%ERRORLEVEL%"

ECHO(
ECHO THE ERRORLEVEL VALUE RETURNED BY CHOICE.EXE IS %CHOICECODE%

IF %CHOICECODE% EQU 2 (SET "CAMFORLARGEPARTS=NO") ELSE SET "CAMFORLARGEPARTS=YES"

ECHO(
ECHO THE VALUE SET FOR VARIABLE CAMFORLARGEPARTS IS %CAMFORLARGEPARTS%

PAUSE

EXIT /B

:SKIPTHIS
…

因为你的 choice 命令只有 2 个选项,一个简单的方法是使用 else 语句并且只计算 errorlevel 2

重要的是在 choiceif errorlevel

之间没有任何其他命令
@echo off
CHOICE /C YN /N /T 10 /D Y /M "USE LARGE PARTS? -> PRESS 'Y' FOR YES | 'N' FOR NO"

IF ERRORLEVEL 2 (
    SET "CAMFORLARGEPARTS=NO"
) else (
    set "CAMFORLARGEPARTS=YES"
)
echo %CAMFORLARGEPARTS%

或者您仍然可以计算每个 errorlevel 并简单地使用 goto 语句。这必须按降序排列,否则它将不起作用:

@echo off
CHOICE /C YN /N /T 10 /D Y /M "USE LARGE PARTS? -> PRESS 'Y' FOR YES | 'N' FOR NO"

If ERRORLEVEL 2 goto :CAM2
If ERRORLEVEL 1 goto :CAM1
If ERRORLEVEL 0 exit /b
goto :EOF  
:CAM2
SET "CAMFORLARGEPARTS=NO"
goto :result
:CAM1
SET "CAMFORLARGEPARTS=YES"
:result
echo %CAMFORLARGEPARTS%

严格来说不是答案,因为发布的原始代码没有问题。

测试代码:

@ECHO Off
SETLOCAL
cls

ECHO.&ECHO --- As posted --- but with ECHO turned off

CHOICE /C YN /N /T 10 /D Y /M "Use Large Parts? -> Press 'Y' for Yes | 'N' for No
ECHO.
ECHO The errorlevel value returned by choice.exe is %ERRORLEVEL%

IF ERRORLEVEL 1 SET CAMFORLARGEPARTS=YES
IF ERRORLEVEL 2 SET CAMFORLARGEPARTS=NO
ECHO.
ECHO The value set for variable CAMFORLARGEPARTS is %CAMFORLARGEPARTS%
CHOICE /C YN /N /T 10 /D Y /M "Use Large Parts? -> Press 'Y' for Yes | 'N' for No
ECHO.
ECHO The errorlevel value returned by choice.exe is %ERRORLEVEL%

IF ERRORLEVEL 1 SET CAMFORLARGEPARTS=YES
IF ERRORLEVEL 2 SET CAMFORLARGEPARTS=NO
ECHO.
ECHO The value set for variable CAMFORLARGEPARTS is %CAMFORLARGEPARTS%

echo.&ECHO --- As posted but testing directly after choice

CHOICE /C YN /N /T 10 /D Y /M "Use Large Parts? -> Press 'Y' for Yes | 'N' for No
IF ERRORLEVEL 1 SET CAMFORLARGEPARTS=YES
IF ERRORLEVEL 2 SET CAMFORLARGEPARTS=NO
ECHO.
ECHO The value set for variable CAMFORLARGEPARTS is %CAMFORLARGEPARTS%
CHOICE /C YN /N /T 10 /D Y /M "Use Large Parts? -> Press 'Y' for Yes | 'N' for No

IF ERRORLEVEL 1 SET CAMFORLARGEPARTS=YES
IF ERRORLEVEL 2 SET CAMFORLARGEPARTS=NO
ECHO.
ECHO The value set for variable CAMFORLARGEPARTS is %CAMFORLARGEPARTS%

echo.&ECHO Now testing ERRORLEVEL changes with intervening ECHO

call :seterr 8
ECHO ERRORLEVEL is %errorlevel%
ECHO ERRORLEVEL is %errorlevel%
echo.&ECHO But testing ERRORLEVEL changes with intervening CALL ECHO
CALL ECHO ERRORLEVEL is %%errorlevel%%
ECHO ERRORLEVEL is %errorlevel%
echo.&ECHO But testing ERRORLEVEL changes with intervening different CALL ECHO
call :seterr 7
CALL ECHO ERRORLEVEL is %errorlevel%
ECHO ERRORLEVEL is %errorlevel%

echo.&ECHO Now testing ERRORLEVEL changes with intervening SET

call :seterr 6
ECHO ERRORLEVEL is %ERRORLEVEL%
SET zz=61
ECHO ERRORLEVEL is %errorlevel%

echo.&ECHO Now testing ERRORLEVEL changes with intervening SET /A

call :seterr 5
ECHO ERRORLEVEL is %ERRORLEVEL%
SET /a zz=616
ECHO ERRORLEVEL is %errorlevel%

PAUSE
GOTO :eof

:seterr
EXIT /B%1

结果:

所以 OP 的测试场景似乎有问题...我想我看到了。

代码操作并显示 CAMFORLARGEPARTS 但显示的是 SOLIDCAMFORLARGEPARTS