Page 1 of 1

Redirect Twice

Posted: 25 May 2010 14:04
by Eagle710
I am wondering if there is a way to redirect the output to both a text document and the console/command prompt so i can watch the progress.

Example:
test.bat

Code: Select all

dir C:\Temp


Contents of C:\Temp-----Test1, Test2, Test3

Command Line:
test.bat > output.txt --> Place output in txt file and I would also like the results to be displayed on the screen at the same time.

Re: Redirect Twice

Posted: 25 May 2010 14:27
by aGerman
Eagle710,

you can't redirect the stdOut of a command twice. But you could call the command as child process into a for loop to get each line to a dynamic variable. This variable you could echo twice.

Code: Select all

@echo off &setlocal
>output.txt type nul
for /f "delims=: tokens=1*" %%a in ('dir C:\Temp^|findstr /n "^"') do (
  >>output.txt echo.%%b
  echo.%%b
)
pause


Regards
aGerman

Re: Redirect Twice

Posted: 25 May 2010 15:06
by !k

Re: Redirect Twice

Posted: 26 May 2010 11:39
by avery_larry
One "poor man's" pure dos way to accomplish that is to capture the output into a variable, then echo the variable to the screen, and then echo the variable to the file. Multiple steps and a for loop make most people shy away.

Re: Redirect Twice

Posted: 27 May 2010 14:45
by jeb
I suppose, I am such a "poor man" who try to solve it with pure dos batch.

Because, if I want to solve it easy or short or ..., I shouldn't take dos batch,
there are very good alternatives like python, php, perl or c (sorry gostmachine, vbs is not such an alternative) :)

For me it's fun to find solutions with pure batch, and "Yes, We Can!"

Jan Erik

Re: Redirect Twice

Posted: 16 Mar 2011 10:36
by knightEknight
REM - another option is to write your own :echo

@echo off

call :echo test text
exit/b

:echo
echo %*
echo %* >> outfile.txt
exit/b

:end