我试图在不使用choice
和set /p
的情况下获得y / n按键。我已经这样使用过del
:
@echo off
copy /y nul $~temp.txt
del /p $~temp.txt >nul
if exist $~temp.txt (
echo/
echo You pressed y.
) else (
echo/
echo You pressed n.
)
pause >nul
它不直接进行按键操作。之后,我们必须按Enter键。不按回车键的任何方法都会受到赞赏。
xcopy
,当指定了xcopy
选项时,它提示是否继续复制; /W
开关可防止实际复制任何内容:/L
或者,您可以(错误地)使用@echo off rem // Capture first line of output of `xcopy /W`, which contains the pressed key: for /F "delims=" %%K in (' xcopy /W /L "%~f0" "%TEMP%" ') do set "KEY=%%K" & goto :NEXT :NEXT rem // Extract last character from captured line, which is the pressed key: set "KEY=%KEY:~-1%" rem // Return pressed key: echo You pressed "%KEY%". rem // Check pressed key (example): if /I not "%KEY%"=="Y" if /I not "%KEY%"=="N" >&2 echo You pressed an unexpected key!
,当指定了replace
选项时,它提示是否继续替换;replace
开关可防止在源和目标相同的情况下实际复制任何内容:
/W
我不得不承认我不记得/U
在Windows XP上是否可用。
@echo off
rem // Capture second line of output of `replace /W`, which is the pressed key:
for /F "skip=1 delims=" %%K in ('
replace /W /U "%~f0" "."
') do set "KEY=%%K" & goto :NEXT
:NEXT
rem // Return pressed key:
echo You pressed "%KEY%".
rem // Check pressed key (example):
if /I not "%KEY%"=="Y" if /I not "%KEY%"=="N" >&2 echo You pressed an unexpected key!
就是这种方式:replace
这对我有用,因为sc
帮助屏幕中直接显示了y / n键提示。