批处理文件:在执行其他命令期间获取用户输入,然后稍后检查

Batch File: Getting user input during execution of other commands, then checking it later

我想要的是与 Windows 7、8.1 和 10 一起使用的命令(或一系列命令)。它需要在执行其所在的批处理文件期间随时收集用户输入,只是为了稍后检查和解释。我不介意使用输入文本文件。

我已经搜索过答案,但我最接近的答案是 <nul set /p "input=",但它要求用户在命令为 运行。任何帮助将不胜感激。

此方法利用 GOTO 创建一个循环,每 6 秒左右检查一次 input.txt 的第一行。您可以用任何您想要的内容替换 :DOSTUFF 的内容。如果您有任何问题,请告诉我。

@echo off
GOTO DOSTUFF

:CHECKINPUT
    for /f %%a in (input.txt) do (
        if %%a NEQ "" (
            set "input=%%a"
            GOTO GOTINPUT
            )
        exit /b
    )
    GOTO DOSTUFF

:GOTINPUT
    echo Thanks for the input!
    echo Here is what you entered:
    echo %input%
    GOTO ENDER
:DOSTUFF
    echo I could be doing other things here, but instead I'm just waiting for input...
    PING 127.0.0.1 -n 6 >nul
    GOTO CHECKINPUT
:ENDER
    pause

虽然这是 运行 在一个 window 中,但我只是在另一个命令提示符中 运行 echo test>input.txt

为了使它更可靠,您可能需要在检查文件后覆盖它。这可以通过 echo.>input.txt

轻松完成