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

我只是一个虾纸丫 提交于 2019-12-18 05:49:33

问题


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

I would like to do something inside the script to redirect the output to a file
For example:

foo.bat

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

回答1:


One way of doing it is the following. Use the call command to execute a label in the script. Edit I 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:

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

:testitout
echo Hi There
echo Goodbye



回答2:


> is the standard, so you're more-or-less stuck with that; however, you can move it inside the batch file:

foo.bat:

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


来源:https://stackoverflow.com/questions/4607390/is-it-possible-to-redirect-the-output-of-a-batch-file-inside-the-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!