Search code examples
batch-filewindows-server-2008dos

redirect standard output to both screen and text file


I'm writing a dos batch script and one thing I want to do is to redirect a command line out to both standard output and a text file. I can do either, but not both. Also, I can't call it with another batch script or call it at command line.

the command I want to execute is reg add "%regKey%" /f and the output is The operation completed sucessfully. and I want it to display on standard output and append in the text file.

So far, I have tried:

reg add "%regKey%" /f>>%logfile%>CON <-- only appended to textfile

reg add "%regKey%" /f>CON>>%logfile% <-- only displayed in standard output

I saw this SO, but it's for bash.


Solution

  • reg add "%regKey%" /f>>%logfile%
    type %logfile%
    

    Which is easier to understand for yourself, and anyone else that will come along looking at your script.