Ctrl+C 无法可靠地终止 Windows 批处理脚本

Dyl*_*reb 5 windows cmd batch-file

我正在尝试编写一个批处理脚本,提示用户输入一个目录并将多个文件从该目录复制到另一个目录。为此,我有一个简单的循环,使用标签和set /p输入。问题是,尝试通过按下来摆脱这个循环ctrl+c并不总是有效。

:checkloop

@set /p READMEPATH=Please enter the location of readme.txt: 

@if not exist "%READMEPATH%\README.txt" goto somethingmissing

@goto allfound

:somethingmissing
@echo Can't find readme in directory "%READMEPATH%"
@goto checkloop

:allfound

@echo Okay, found
Run Code Online (Sandbox Code Playgroud)

示例输出类似于

Please enter the location of readme.txt:
Can't find readme in directory ""
Please enter the location of readme.txt: ^CCan't find readme in directory ""
Please enter the location of readme.txt: ^CCan't find readme in directory ""
Please enter the location of readme.txt: ^CCan't find readme in directory ""
Please enter the location of readme.txt: ^CCan't find readme in directory ""
Please enter the location of readme.txt: ^CCan't find readme in directory ""
Please enter the location of readme.txt: ^CTerminate batch job (Y/N)? y
Run Code Online (Sandbox Code Playgroud)

反复按 ctrl+c 就好像按 Enter 键一样,脚本继续执行,直到最终起作用。所需的尝试次数是可变的;有时它会立即起作用,有时 2-3 次,很少需要 7 次以上。

操作系统是Windows 10。

jeb*_*jeb 2

您可以检查 READMEPATH 是否已定义。

:checkloop
set "READMEPATH="
set /p READMEPATH=Please enter the location of readme.txt: 
if not defined READMEPATH exit /b
Run Code Online (Sandbox Code Playgroud)

但这无法检测对于空行按 CTRL-C 和 ENTER 之间的区别。