简体   繁体   English

批量文件在循环中运行Windows命令

[英]Batch file to run windows command in loop

the following below code opens 10 command prompt windows. 以下代码打开10个命令提示符窗口。 But I am trying to finish one command at a time and start the next in the same window. 但我试图一次完成一个命令,然后在同一个窗口中启动下一个命令。 Can anyone please suggest changes? 有人可以建议改变吗?

echo off
SET /a i=0

:loop
IF %i%==10 GOTO END
echo This is iteration %i%. 
START cmd.exe /K "cd C:\bin\phantomjs-1.9.2-windows & phantomjs examples\loadspeed.js"
SET /a i=%i%+1
GOTO LOOP

:end
echo

Using your approach, you can use start /wait and cmd /c in combination to open ten windows one at a time. 使用您的方法,您可以组合使用start /waitcmd /c一次打开十个窗口。 The /wait pauses the batch files execution until the new CMD window closes. /wait暂停批处理文件的执行,直到新的CMD窗口关闭。 /c tells the CMD window to close when it finishes processing the quoted commands. /c告诉CMD窗口在完成处理引用命令时关闭。

START /wait cmd.exe /c "cd C:\bin\phantomjs-1.9.2-windows & phantomjs examples\loadspeed.js"

If you want to run the ten iterations in a single new window, then you need to change your command to something like this: 如果要在单个窗口中运行十次迭代,则需要将命令更改为以下内容:

@echo off
start /wait cmd /c "@echo off&&for /L %%i in (1,1,10) do (echo Iteration: %%i&&ping -n 2 localhost)"

This opens a new CMD window and then executes the echo and ping statements 10 times, each time waiting until the prior set finishes before moving to the next iteration. 这将打开一个新的CMD窗口,然后执行echoping语句10次,每次等到上一次设置完成后再移动到下一次迭代。

This should execute it 10 times, one after another. 这应该一个接一个地执行10次。

@echo off
cd /d "C:\bin\phantomjs-1.9.2-windows"
for /L %%a in (1,1,10) do (
   echo This is iteration %%a
   start "" /w /b "phantomjs" "examples\loadspeed.js"
)
pause

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM