好的,所以我正在尝试自动化基于ARM的CPU压力测试程序(它通过命令提示符运行,并且需要相当多的用户输入)。我想做的是,使用for /f
观察输出,并在看到各自的提示字符串时运行一些不同的sendkeys脚本。我已经尝试制作两个非常准确的批处理文件来测试它:
@echo off
REM This is a file that asks for inputs
set /p q1="Please press 1: "
echo.
set /p q2="Please press 2: "
echo.
set /p q3="Please press 3: "
echo.
echo All buttons have been pressed,
echo.
echo button 1 was: %q1%
echo button 2 was: %q2%
echo button 3 was: %q3%
echo.
set /p foo="Press Enter to finish..."
@echo off
echo We will now launch the input command
echo.
timeout .5
echo in 5...
timeout 1
echo 4...
timeout 1
echo 3...
timeout 1
echo 2...
timeout 1
echo 1...
timeout 1
echo Launching...
for /f "delims= " %%i in ('Input.bat ^| find /i "Please press 1:"') do (
echo we did it
)
echo Did you make the right decisions?
set /p foo=
我得到的结果就是“启动...”Echo之后的空白命令提示符。如果我按Enter键四次,它会回复“我们做到了”回声以及“你做出了正确的决定吗?”回声。所以,最后我的问题。有没有办法让for /f
不再重定向stdout,以及有什么方法可以在命令运行时让for /f () do ()
发生?因此,根据您的请求,听起来您正在尝试从新的batch1
脚本中读取batch2
脚本中的字符串。为此,您必须将变量导出到文本文档。从那里,我们可以阅读文本文档并收集变量。如果我对你的请求完全错误(这有点难以理解你的请求)那么我的寡头垄断,希望这些小技巧可以帮助你至少。
要导出文件,您需要使用>>
例如:Echo This will be line one! >> Yourfile.txt
另请注意,完成脚本后,请使用goto :eof
退出。
这是您的第一批文件:
@ECHO OFF
@DEL /Q %~dp0\strings.txt
REM This batch file asks for inputs
set /p q1="Please press 1: "
echo %q1% >> strings.txt
echo.
set /p q2="Please press 2: "
echo %q2% >> strings.txt
echo.
set /p q3="Please press 3: "
echo %q3% >> strings.txt
echo.
echo All buttons have been pressed,
echo.
echo button 1 was: %q1%
echo button 2 was: %q2%
echo button 3 was: %q3%
echo.
set /p foo="Press Enter to finish..."
goto :eof
这是您的第二批文件:
@ECHO OFF
echo We will now launch the input command.
echo.
echo in 5...
PING localhost -n 2 >nul
echo 4...
PING localhost -n 2 >nul
echo 3...
PING localhost -n 2 >nul
echo 2...
PING localhost -n 2 >nul
echo 1...
PING localhost -n 2 >nul
CLS
echo Launching...
:: Do action for each string. Use %%G to call the variable.
for /f "delims== tokens=*" %%G in (strings.txt) do (
echo Working on string: %%G
)
echo Did you make the right decisions?
pause > nul
DEL /Q %~dp0\strings.txt
goto :eof
请记住,不是使用timeout 1
你实际上可以使用PING localhost -n 2 >nul
所以它实际上并没有冻结它自己的提示。