重定向整个.bat文件内部的流

时间:2014-07-03 06:58:05

标签: batch-file

我最近开始使用.bat文件,我试图将输出重定向到文件。

到目前为止,我找到了两个选项:

  1. echo aaa > out.txt - 将单个echo命令的输出发送到指定文件(也可以使用>>附加)
  2. 使用somefile.bat > out.txt从cmd调用整个文件(实际上类似于数字1,因为它将单个命令somefile.bat的输出发送到out.txt
  3. 我正在寻找的是其他东西 - 我试图在我的文件中添加一行,将该点的所有输出发送到文件。

    谢谢!

2 个答案:

答案 0 :(得分:1)

没有通用的解决方案。这取决于批处理文件的要求。

对于很多批处理文件,Stephan的答案可以毫无问题地工作,考虑到他指出的内容:所有代码都在一个块内,其中的任何变量管理都可能需要延迟扩展。

其他替代方法是在子程序下移动代码,使用重定向调用

@echo off
    call :mainProcess %* > outputFile
    exit /b

:mainProcess
    :: here the batch file begins
    echo %1 %2 %3

答案 1 :(得分:0)

echo this goes to screen
(
echo this line goes to the file
echo also this line and the ping-output
ping www.stackoverflow.com
echo and this
)>file.txt
echo this goes to screen again

注意:

块内的所有内容(()之间)都会立即被解析。如果在块内使用变量,则可能需要延迟扩展。