批处理:显示上次用户输入



(这是我在这里的第一篇文章,所以请耐心等待(

你能在批处理文件中显示最后一个用户输入吗?我将尝试在这里保持简单。

@echo off
:menu
echo Type 1 to proceed.
set /p example=
if "%example%" == "1" GOTO :proceed
GOTO :error
:proceed
pause
:error
cls
echo You wrote (last user input), that's not correct.
timeout 30
GOTO :menu

我知道我可以将(最后一个用户输入(替换为 %example%,但随后我必须为每个类别制作自定义错误消息,其中大约有 50 个。使用最后一个输入命令会更容易。

顺便说一下,我已经自学了我所知道的关于批处理的所有知识,所以我的示例现在可能存在重大问题,但它以某种方式起作用。

您可以将所有用户输入集中到一个函数中 (user_input(

:menu1
echo Type 1 to proceed.
call :userInput example
if "%example%" == "1" GOTO :proceed
GOTO :error
:menu2
echo Type 42 to proceed.
call :userInput answer
if "%answer%" == "42" GOTO :proceed
GOTO :error
:userInput
set /p LAST_INPUT=
set "%1=%LAST_INPUT%"
exit /b
:proceed
pause
:error
cls
echo You wrote "%LAST_INPUT%", that's not correct.
timeout 30
GOTO :menu

我不知道没有临时文件该怎么做。要在控制台中编写内容,您需要doskey /history(这将跳过脚本本身的运行(:

@echo off
setlocal enableDelayedExpansion
set "last="
set "but_last="
doskey /history > log.txt
for /f "tokens=* delims=" %%# in (log.txt) do (
set "but_last=!last!"
set "last=%%#"
)
echo "%but_last%"
del /s /q log.txt >nul 2>nul

最新更新