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

Batch file 批处理:显示上次用户输入,batch-file,batch-processing,Batch File,Batch Processing,(这是我在这里的第一篇帖子,请耐心等待) 能否在批处理文件中显示最后一次用户输入?我会尽量保持简单 @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 GO

(这是我在这里的第一篇帖子,请耐心等待)

能否在批处理文件中显示最后一次用户输入?我会尽量保持简单

@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条。使用最后一个输入命令会更容易


顺便说一句,我已经自学了关于批处理的所有知识,所以我的示例现在可能有一些主要问题,但不知怎么回事。

如果没有临时文件,我不知道怎么做。要在控制台中写入内容,您需要
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
: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