batch-file - 我需要帮助,当用户输入非法字符时如何停止关闭此批处理文件

标签 batch-file cmd error-handling

我是一个新手,需要帮助,如何在用户输入任何非法字符时停止关闭此批处理文件...

例如,当用户输入为“;”时或“&&”,并且输入基本上使批处理文件崩溃。

我的代码:

@echo off
cls
:start
taskkill /f /im process1.exe
taskkill /f /im process2.exe
taskkill /f /im process3.exe
taskkill /f /im process4.exe
taskkill /f /im process5.exe
:restart
set choice=
set /p choice="Do you want to run these batch commands again? Type 'y' and press 'Enter' to re-execute the batch commands again or type 'n' and press 'Enter' to terminate: "
if not '%choice%'=='n' if not '%choice%'=='y' goto restart
if '%choice%'=='n' set choice=%choice:~0,1%
if '%choice%'=='y' goto start
:end

谢谢!

最佳答案

输入任何非法字符后,如何停止关闭该批处理文件?

选项1 :

使用if而不是反引号来编写"表达式:

if not "%choice%"=="n" if not "%choice%"=="y" goto restart
if "%choice%"=="n" set choice=%choice:~0,1%
if "%choice%"=="y" goto start

选项2(更好):

使用choice:
choice /c yn /m "Do you want to run these batch commands again? Press [y] or [n]
if errorlevel 2 goto :exit
if errorlevel 1 goto :restart

进一步阅读
  • An A-Z Index of the Windows CMD command line-Windows cmd行相关的所有事物的绝佳引用。
  • choice-接受用户输入到批处理文件。选择允许从键盘捕获单个按键。
  • 关于batch-file - 我需要帮助,当用户输入非法字符时如何停止关闭此批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42987572/

    相关文章:

    batch-file - 带有时间戳的ffmpeg jpg批处理脚本

    java - 使用命令行运行程序和配置文件

    windows - 在 Windows 上关闭 echo 来启动 cmd.exe

    Java react 器返回不同的异常

    java - 你能在 Java 中捕获 "package x does not exist"错误吗?

    Windows 批处理脚本 - 一次执行一个命令

    java - 从命令提示符显示 java 程序的输出并将其复制到文件中

    batch-file - 批处理脚本中的 sleep 函数

    python - 如何删除文件和文件夹但保留目录结构并留下一个空文件而不是文件本身?

    class - 错误: Compile Error: sObject type 'KnowledgeArticleVersion' is not supported.