重定向输出和错误从一个批处理文件中 [英] Redirect stdout and stderr from inside a batch file

查看:771
本文介绍了重定向输出和错误从一个批处理文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从里面重定向一个批处理文件,输出和错误。

Is there a way to redirect stdout and stderr for a batch file from inside it.

我想象像

set STDOUT=stdout.log
echo Some text
a.exe
b.exe
c.exe

如果双方某些文本 A.EXE B的输出。 exe文件 c.exe 会去 stdout.log

这可能吗?

推荐答案

这是更有效比它重定向(追加带)每个命令的命令的整个集合一次重定向。这需要时间并初始化重定向。它可能不是noticable几重定向命令,但是如果在与许多迭代一个循环中完成,它可以成为相当显著

It is more efficient to redirect once for the entire collection of commands than it is to redirect (with append) each individual command. It takes time to intialize the redirection. It may not be noticable for a few redirected commands, but if done in a loop with many iterations, it can become quite significant.

的一种方法是括号内括重定向命令的整个块和重定向括号外

One method is to enclose the entire block of redirected commands within parentheses and redirect outside the parentheses

>stdout.log 2>&1 (
  echo Some text
  a.exe
  b.exe
  c.exe
)

另一个选择是把你的命令在子程序和重定向CALL

Another option is to put your commands in a subroutine and redirect the CALL

call :redirect >stdout.log 2>&1
exit /b

:redirect
echo Some text
a.exe
b.exe
c.exe
exit /b

这篇关于重定向输出和错误从一个批处理文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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