在Windows批处理循环中,如何在继续之前等待生成的进程完成 [英] In Windows batch loop, how to wait for spawned processes to complete before continuing

查看:84
本文介绍了在Windows批处理循环中,如何在继续之前等待生成的进程完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在批处理文件中的循环中触发多个命令,如下所示:

I want to fire off multiple commands within a loop in a batch file like this:

for /l %%x in (20170101,1,20170105) do (
start /wait C:\Progra~1\Amazon\AWSCLI\aws s3 cp s3://bucket1/%%x 
s3://bucket2/%%x --recursive
)

#do something else here only when ALL the above commands complete

开始/等待是否具有等到所有命令完成之后再移至循环后的下一行的效果?

Will the Start /wait have the effect of waiting until all commands complete before moving to next line after the loop?

推荐答案

start /wait不是全局"的,它只是等待启动的过程完成(也许...取决于应用程序的编程方式).您需要的内容(并行启动多个进程,直到最后一个进程完成)可以通过另一种方法来完成:为所有进程提供一个已定义的标题并观察它们:

start /wait isn't "global", it just waits for the started process to finish (maybe... depends on how the application is programmed). What you need (starting several processes in parallel and wait until the last is finished) can be done with a different method: give all your processes a defined title and watch them:

@echo off
setlocal enabledelayedexpansion
for /l %%i in (1,1,10) do (
   start "OneOfMyProcesses" timeout !random:~-1!
)
echo waiting
:loop
timeout 1 >nul
tasklist /v |find "OneOfMyProcesses" >nul && goto :loop
echo all of them are finished

注意:如上所述,这可能对您的应用程序

Note: as mentioned above, this may or may not work with your application

这篇关于在Windows批处理循环中,如何在继续之前等待生成的进程完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆