简体   繁体   English

是否可以在脚本中重定向批处理文件的输出?

[英]Is it possible to redirect the output of a batch file inside the script?

I would like to set the standard output of a batch script to go to a file. 我想将批处理脚本的标准输出设置为转到文件。 I would like to do this inside the script if possible. 如果可能的话,我想在脚本中执行此操作。

Note: I do not want to do this: foo.bat > StdOut.txt 注:不想这样做: foo.bat > StdOut.txt

I would like to do something inside the script to redirect the output to a file 我想在脚本中做一些事情来将输出重定向到文件
For example: 例如:

foo.bat foo.bat

:: Redirect standard output to StdOut.txt
:: Insert batch code to do what I want here.

One way of doing it is the following. 其中一种方法是: Use the call command to execute a label in the script. 使用call命令在脚本中执行标签。 Edit I realized the first version I posted does not seem to work in a cmd.exe prompt (I was using TCC). 编辑我意识到我发布的第一个版本似乎不能在cmd.exe提示符下工作(我使用的是TCC)。 The following seems to work in both command processors: 以下似乎适用于两个命令处理器:

@echo off
call :testitout > t.tmp
goto:eof

:testitout
echo Hi There
echo Goodbye

> is the standard, so you're more-or-less stuck with that; >是标准,所以你或多或少地坚持下去; however, you can move it inside the batch file: 但是,您可以在批处理文件中移动它:

foo.bat: foo.bat:

@echo off
@echo Start File > StdOut.txt
@dir >> StdOut.txt
@echo End File >> StdOut.txt

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

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