How to save output from the command line

I know the general syntax for saving output to a text file when executed on the command line.

But my need is different. I want to see it in a command window and I want to save it in a text file. Usually the use of> c: \ dirlist.txt will be saved in a text file, but cannot be seen on the command line window.

I need to see the command line and also save in a text file. Any help

+4
source share
3 answers

Firstly, it is relatively easy to write the TEE.BAT program. The only problem is that the set /p command (used to get the output from the command) does not distinguish the end of the file from the empty line. This means that the output will be written to the first empty line (or the end of the 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 can check if this batch file works this way:

 tee CopyOfFile.txt < File.txt 

However , when this batch file is used in the way it was designed, it fails even now:

 type File.txt | tee CopyOfFile.txt 

The previous line sometimes works fine, and sometimes it shows and saves only the first line of the file. I have done some tests and cannot isolate the cause of the error. Perhaps someone (jeb?) Could explain what happens in this case and give a solution.

+2
source

You want the equivalent of tee 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 can redirect the output to a file and watch this file using the real-time viewer from another command line (i.e., the tail equivalent).

+1
source

You can write your own tee with a batch like Aacini, but without flaws.

 @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 ) 

Initially, the idea consists of walid2me echo to the screen and the file in one line

0
source

Source: https://habr.com/ru/post/1384747/


All Articles