windows 是否可以在脚本内重定向批处理文件的输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4607390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to redirect the output of a batch file inside the script?
提问by SwDevMan81
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 notwant 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.
回答by Mark Wilkins
One way of doing it is the following. Use the call
command to execute a label in the script. EditI realized the first version I posted does not seem to work in a cmd.exe prompt (I was using TCC). The following seems to work in both command processors:
一种方法如下。使用该call
命令执行脚本中的标签。 编辑我意识到我发布的第一个版本似乎在 cmd.exe 提示符下不起作用(我使用的是 TCC)。以下似乎适用于两个命令处理器:
@echo off
call :testitout > t.tmp
goto:eof
:testitout
echo Hi There
echo Goodbye
回答by Chris Adams
> 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