简体   繁体   English

批处理脚本以并行运行文件中的命令

[英]Batch script to run commands from file in parallel

I need to use a cmd tool that is very inneficient. 我需要使用效率非常低的cmd工具。 It usually takes a lot to return the prompt, or it hangs. 返回提示通常会花费很多时间,否则挂起。

I want to automate this process by making aa new batch file that takes the commands from an input.txt file and makes parallel calls to this batch to not waste time, and log everything in a log file. 我想通过制作一个新的批处理文件来自动执行此过程,该批处理文件从input.txt文件中获取命令并对该批处理进行并行调用,以免浪费时间,并将所有内容记录在日志文件中。

This is as far as I got 据我所知

@echo off
for /f "tokens=*" %%a in (input.txt) do (
  START %%a >> result.txt
  TIMEOUT /T 60
)
PAUSE

But I get the error: 但是我得到了错误:

The process cannot access the file because it is being used by another process.

which I presume it's referring to the redirection to results.txt, because the previous command is still running. 我认为这是指重定向到results.txt,因为先前的命令仍在运行。

Besides that it fills my PC with CMDs. 除此之外,它还用CMD填充了我的PC。

Can anyone please help me with this? 谁能帮我这个忙吗?

That's a bit confusing. 这有点令人困惑。 You want your commands to be executed in parallel but you don't want them to be executed in different CMD windows? 您是否希望并行执行命令,但又不想在不同的CMD窗口中执行命令? You should try to add /B: 您应该尝试添加/ B:

START /B %%a

This will make the CMDs spawn "invisible" - means in background. 这将使CMD生成为“隐形”-表示背景。 To avoid access violation errors, you could move the redirection from your bat file to the txt file by appending the redirection to each line. 为避免访问冲突错误,可以通过将重定向附加到每一行来将重定向从bat文件移动到txt文件。 Say your txt looks like this: 说您的txt如下所示:

...
ping 127.0.0.1
copy C:\xyz.txt d:\xyz.txt
...

The modified file will look like this: 修改后的文件将如下所示:

...
ping 127.0.0.1>>result.txt
copy C:\xyz.txt d:\xyz.txt>>result.txt
...

This looks similar but there is a big difference. 看起来相似,但有很大的不同。 START "ping 127.0.0.1" >> result.txt means write the result of START ... into your text file while START "ping 127.0.0.1>>result.txt" means write the result of the ping command into the file. START "ping 127.0.0.1" >> result.txt表示将START ...的结果写入文本文件,而START "ping 127.0.0.1>>result.txt"表示将ping命令的结果写入文件。

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

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