保持`for / f`重定向输出/在命令期间发生`do()`

问题描述 投票:0回答:1

好的,所以我正在尝试自动化基于ARM的CPU压力测试程序(它通过命令提示符运行,并且需要相当多的用户输入)。我想做的是,使用for /f观察输出,并在看到各自的提示字符串时运行一些不同的sendkeys脚本。我已经尝试制作两个非常准确的批处理文件来测试它:

  • 第一个是一个简单的批处理文件,要求3个独立的输入 @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..."
  • 第二个是运行第一个(^)的批处理文件,并在输出中查找“Please Press 1:” @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 ()发生?
windows batch-file cmd
1个回答
0
投票

因此,根据您的请求,听起来您正在尝试从新的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所以它实际上并没有冻结它自己的提示。

© www.soinside.com 2019 - 2024. All rights reserved.