异步执行的命令,同时将输出重定向到一个文件中的批处理文件?(Execute a command as

2019-07-18 18:18发布

让我们在批处理文件中说,我想要异步执行mycommand的 (而无需等待其完成)。 而且我不希望在一个新的控制台窗口执行mycommand的

同时,我想重定向mycommand的输出到output.txt

因此,在批处理文件,如果我写

START myCommand > output.txt

output.txt的将是空的,我会看到一个新的窗口。

如果我写

myCommand > output.txt

然后我无法异步执行它。

有没有什么办法可以实现所有这三个要求? (异步,没有新的窗口,输出重定向)

谢谢!

Answer 1:

我还没有完全测试,但我认为这可能工作:

start /b "" myCommand >output.txt

我认为这两种形式工作的优良-唯一的区别是,如果标准错误重定向,以及和START启动失败mycommand的。

重定向START:既mycommand的和START输出重定向到文件。

start /b "" myCommand >output.txt >2&1

重定向mycommand的只有:只有mycommand的输出重定向。 任何启动错误消息将出现在屏幕上。 请注意,我选择逃避重定向,而不是使用引号像杰布 。

start /b "" myCommand ^>output.txt ^>2^&1


Answer 2:

几乎像dbenhams回答,但你需要重定向强制新的线程,而不是在start命令。

start "myTitle" "myCommand > output.txt"


文章来源: Execute a command asynchronously while redirecting the output to a file in a batch file?