批次:显示最后一个用户输入

问题描述:

(这是我在这里的第一篇文章,所以大家多多包涵)批次:显示最后一个用户输入

你能告诉在批处理文件中的最后一个用户输入?我会尽量在这里保持简单。

@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 

我知道我可以取代%范例%(最后用户输入),但后来我不得不做出的自定义错误消息每个类别,并有他们的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 
+0

是的。这比较好。+ 1 – npocmaka

我不知道怎么做没有临时文件。要获得书面INT控制台的事情,你需要的​​(这将跳过脚本本身的运行):

@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 
+0

好解决方案,但也许有点矫枉过正:-) – jeb