4

I know the general syntax to save the output to a text file while executing in command prompt.

But my need is different. I want to see it in the Command prompt window also I want to save it in text file. USually using > c:\dirlist.txt will save to text file but cant see in command prompt window.

I need to see in command prompt as well as save in text file. Any help

1
  • Voted to transfer the question to SuperUser, since it's more appropriate for that site. Commented Dec 5, 2011 at 18:10

4 Answers 4

2

At first, it is relatively easy to write a TEE.BAT program. The only problem is that set /p command (used to get the output from the command) doesn't differentiate between the end of file and an empty line. This mean that the output will be captured until the first empty line (or the end of file):

@echo off
:: usage: AnyCommand | TEE filename
setlocal EnableDelayedExpansion
if exist %1 del %1
:nextLine
set line=:EOF
set /p line=
if "!line!" == ":EOF" goto :eof
echo(!line!
echo(!line!>> %1
goto nextLine

You may test that this Batch file indeed works this way:

tee CopyOfFile.txt < File.txt

However, when this Batch file is used in the way it was designed, it fails now and then:

type File.txt | tee CopyOfFile.txt

Previous line sometimes works ok and sometimes show and store just the first line of the file. I made several tests and can not isolate the cause of the error. Perhaps someone (jeb?) could explain what is happening in this case and give a solution.

Sign up to request clarification or add additional context in comments.

2 Comments

I examine it at How Set/p works. set/p doesn't wait for the data from a pipe, and set/p can't read data with <LF> only, but pipes remove the <CR>
My last sentence is nonsense. The pipe buffer contains the CR/LF pairs, set/p reads always the complete (pipe) buffer, but removes all after the first CR/LF, so you can lose lines
1

You want the equivalent of the tee program for Windows. If you can use PowerShell, then you already have what you need. Otherwise, google for an implementation that works on your system.

Alternatively, you could redirect the output to a file and observe that file with a live log viewer from another command prompt (i.e. the equivalent of tail).

Comments

0

You can write your own tee with batch, like the one of Aacini, but without the drawbacks.

@ECHO OFF
setlocal DisableDelayedExpansion
for /F "delims=" %%A in ('findstr /n "^" ') do (
  set "line=%%A"
  setlocal EnableDelayedExpansion
  set "line=!line:*:=!"
  (echo(!line!)
  (echo(!line!) > "%~1"
  endlocal
)

The initially idea is from walid2me echo to screen and file in a single line

Comments

0

The following is another approach for displaying and Logging command output:

@Echo off & CD "%~dp0"
For /F "UsebackQ Tokens=* Delims=" %%A in (`"%~1"`) do (
    >>Outputfile.txt Echo(%%A
    Echo(%%A
)
Goto :EOF

1 Comment

Used either as a subroutine or stand alone batch file, and call it with the command to be output in qouted form. Depending on quoting within the command, it may be necessary to assign the command to a variable first and pass the variable in the call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.