如何直接使用y / n键而不使用选择和设置/ p

问题描述 投票:0回答:2

我试图在不使用choiceset /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键。不按回车键的任何方法都会受到赞赏。

windows batch-file keypress
2个回答
3
投票
您可以(错误地)使用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上是否可用。

0
投票
经过一些研究,我发现使用@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键提示。
© www.soinside.com 2019 - 2024. All rights reserved.