Windows 使用启动的批处理文件不在后台放置多个程序

Windows batch file using start not placing multiple programs in background

我正在尝试从 sql 服务器和 ldap 服务器为多个客户端获取数据。我需要先获取 sql 数据,然后再获取 ldap 数据。在 Unix shell 中,直接围绕子进程进行循环,为每个客户端进行两次检索,然后等待它完成。作为一个 windows 批处理文件,它是按顺序发生的。 IE。在我为一个客户检索数据之前,它不会转到下一个客户。如何同时获取每个客户的数据?这是我拥有的:

REM Get DB and client info. from config file
for /F "tokens=1,2,3,4,5,6 delims=| eol=#" %%G in (%cfg%\%env%.data) do (
    REM Mark file as being in process of receiving data
    type nul > %%K.tmp

    REM Get data and remove tmp file to indicate completion
    start cmd /C sqlcmd .... -Q "A long query" ^> %%K.dat1 && "c:\Program Files\Softerra\LDAP Administrator 4\laimex.exe" ... /sql "Another query" > %%K.dat2 && del %%K.tmp
)

出于某种原因,我需要将第一个重定向转义为 ^>,而后一个则不需要。在这一点上,我假设所有内容都将在后台检索,并且之后我需要检查进程何时完成,我将通过检查我创建的零字节临时文件是否存在来完成。但是发生的是,循环中的每次迭代仅在前一个迭代完成时才开始,而不是通过放置在后台立即发生。谁能建议我如何解决这个问题?

您还需要转义 && (^&^&),否则它会在启动后立即执行它之后的所有内容。示例:

1 在新的 shell 中正确执行,而 2 接管主要 window(不是你想要的)。

start cmd /C ping 127.0.0.1 && ping 127.0.0.2

两者都在一个新的 window 中一个接一个地执行。

start cmd /C ping 127.0.0.1 ^&^& ping 127.0.0.2

同上,另一种方法。

start cmd /C "ping 127.0.0.1 && ping 127.0.0.2"

同时转义其他 > 的,这可能有效:

start cmd /C sqlcmd .... -Q "A long query" ^> %%K.dat1 ^&^& "c:\Program Files\Softerra\LDAP Administrator 4\laimex.exe" ... /sql "Another query" ^> %%K.dat2 ^&^& del %%K.tmp