Execute a command asynchronously while redirecting

2019-02-19 17:14发布

Let's say in a batch file, I want to execute myCommand asynchronously (without waiting for it to finish). And I don't want to execute myCommand in a new console window.

At the same time, I want to redirect the output of myCommand to output.txt

So in the batch file, if I write

START myCommand > output.txt

output.txt will be empty and I will see a new window.

If I write

myCommand > output.txt

then I cannot execute it asynchronously.

Is there any way I can achieve all these three requirements? (asynchronously, no new window, redirect output)

Thanks!

2条回答
Fickle 薄情
2楼-- · 2019-02-19 17:58

Nearly like dbenhams answer, but you need to force the redirection to the new thread, not to the start command.

start "myTitle" "myCommand > output.txt"
查看更多
Juvenile、少年°
3楼-- · 2019-02-19 18:12

I haven't tested it fully, but I think this may work:

start /b "" myCommand >output.txt

I believe both forms work fine - the only difference is if standard error is redirected as well and START fails to launch myCommand.

Redirecting START: both myCommand and START output are redirected to the file.

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

Redirecting myCommand only: Only myCommand output is redirected. Any START error message will appear on the screen. Note, I opted to escape the redirection instead of using quotes like jeb.

start /b "" myCommand ^>output.txt ^>2^&1
查看更多
登录 后发表回答