我不能 运行 这个批处理脚本,它在第一个命令后停止
I can't run this batch script, it stops after the first command
我正在尝试使用批处理脚本方法制作卸载程序。我为此编写了这个程序:
@echo off
set /p res = Are you sure to uninstall my summer car and all of its
components? (NO = N, YES = Y)
if %res%==Y ( goto un )
else if %res%==y ( goto un )
else if %res%==N ( exit )
else if %res%==n ( exit )
:un
rmdir /s /q mysummercar
pause
但我的程序实际上并没有不工作,它显示 "Are you sure to uninstall my summer car and of its components (NO = N, YES = Y)",但是当我按 Y(或小写的 y)时出现此消息后,批处理文件消失了。它应该仅在按下 N(或小写的 n)或其他键时消失,但在按下 Y 时它也会消失。
任何人都可以告诉我 运行 完整批处理文件的解决方案,即 运行 程序的 "un" 循环吗?
在学习了如何使用 If
、Set
和 Choice
命令后,您可以选择将代码简化为更合理的代码:
使用Set /P
@If Not Exist "mysummercar\" Exit /B
@Set /P "res=Uninstall my summer car and all of its components [Y,N]?"
@If /I Not "%res%"=="Y" Exit /B
@RD /S/Q "mysummercar"
或者稍微短一点,(没有Not
):
@If Not Exist "mysummercar\" Exit /B
@Set /P "res=Uninstall my summer car and all of its components [Y,N]?"
@If /I "%res%"=="Y" RD /S/Q "mysummercar"
使用Choice
@If Not Exist "mysummercar\" Exit /B
@Choice /M "Uninstall my summer car and all of its components"
@If ErrorLevel 2 Exit /B
@RD /S/Q "mysummercar"
或者稍微短一点,(with Not
):
@If Not Exist "mysummercar\" Exit /B
@Choice /M "Uninstall my summer car and all of its components"
@If Not ErrorLevel 2 RD /S/Q "mysummercar"
有更好的方法来做到这一点,但要专注于您当前的格式...这里的主要问题是您在 =
之前创建一个带有 space 的变量,因此变量创建为 %res %
,因此 %res%
不是 %res %
此外,我们使用 if /i
开关来测试不区分大小写。最后,如果用户键入任何其他值会发生什么?然后它将失败并仍然执行 :un
因此只是 运行 if /i "%res%"=="y"
@echo off
set /p "res=Are you sure to uninstall my summer car and all of its components? (NO = N, YES = Y): "
if /i "%res%"=="y" rmdir /s /q mysummercar
然而,一个更简单的解决方案是:
@echo off
choice /C YN /M "Press Y to uninstall my summer car or N to Cancel"
if %errorlevel% equ 1 rmdir /s /q mysummercar
我强烈建议阅读一些帮助文件。打开 cmd.exe
和 运行 以下内容:
choice /?
set /?
if /?
我正在尝试使用批处理脚本方法制作卸载程序。我为此编写了这个程序:
@echo off
set /p res = Are you sure to uninstall my summer car and all of its
components? (NO = N, YES = Y)
if %res%==Y ( goto un )
else if %res%==y ( goto un )
else if %res%==N ( exit )
else if %res%==n ( exit )
:un
rmdir /s /q mysummercar
pause
但我的程序实际上并没有不工作,它显示 "Are you sure to uninstall my summer car and of its components (NO = N, YES = Y)",但是当我按 Y(或小写的 y)时出现此消息后,批处理文件消失了。它应该仅在按下 N(或小写的 n)或其他键时消失,但在按下 Y 时它也会消失。 任何人都可以告诉我 运行 完整批处理文件的解决方案,即 运行 程序的 "un" 循环吗?
在学习了如何使用 If
、Set
和 Choice
命令后,您可以选择将代码简化为更合理的代码:
使用Set /P
@If Not Exist "mysummercar\" Exit /B
@Set /P "res=Uninstall my summer car and all of its components [Y,N]?"
@If /I Not "%res%"=="Y" Exit /B
@RD /S/Q "mysummercar"
或者稍微短一点,(没有Not
):
@If Not Exist "mysummercar\" Exit /B
@Set /P "res=Uninstall my summer car and all of its components [Y,N]?"
@If /I "%res%"=="Y" RD /S/Q "mysummercar"
使用Choice
@If Not Exist "mysummercar\" Exit /B
@Choice /M "Uninstall my summer car and all of its components"
@If ErrorLevel 2 Exit /B
@RD /S/Q "mysummercar"
或者稍微短一点,(with Not
):
@If Not Exist "mysummercar\" Exit /B
@Choice /M "Uninstall my summer car and all of its components"
@If Not ErrorLevel 2 RD /S/Q "mysummercar"
有更好的方法来做到这一点,但要专注于您当前的格式...这里的主要问题是您在 =
之前创建一个带有 space 的变量,因此变量创建为 %res %
,因此 %res%
不是 %res %
此外,我们使用 if /i
开关来测试不区分大小写。最后,如果用户键入任何其他值会发生什么?然后它将失败并仍然执行 :un
因此只是 运行 if /i "%res%"=="y"
@echo off
set /p "res=Are you sure to uninstall my summer car and all of its components? (NO = N, YES = Y): "
if /i "%res%"=="y" rmdir /s /q mysummercar
然而,一个更简单的解决方案是:
@echo off
choice /C YN /M "Press Y to uninstall my summer car or N to Cancel"
if %errorlevel% equ 1 rmdir /s /q mysummercar
我强烈建议阅读一些帮助文件。打开 cmd.exe
和 运行 以下内容:
choice /?
set /?
if /?