Redirect Twice

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Eagle710
Posts: 19
Joined: 29 Mar 2009 17:27

Redirect Twice

#1 Post by Eagle710 » 25 May 2010 14:04

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Redirect Twice

#2 Post by aGerman » 25 May 2010 14:27

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Redirect Twice

#3 Post by !k » 25 May 2010 15:06


avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Redirect Twice

#4 Post by avery_larry » 26 May 2010 11:39

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.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Redirect Twice

#5 Post by jeb » 27 May 2010 14:45

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

knightEknight
Posts: 1
Joined: 16 Mar 2011 10:33

Re: Redirect Twice

#6 Post by knightEknight » 16 Mar 2011 10:36

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

Post Reply