-1

I am trying to emulate the unix port tee in cmd. I am trying this:

tasklist >con >log.txt

But it doesn't work. It just echoes the result not logging into the text file. Please help.

  • [possible duplicate](https://stackoverflow.com/questions/15551379/how-do-i-make-a-log-of-all-echo-commands-in-a-batch-file) and [this](https://stackoverflow.com/questions/21071904/when-adding-a-textlog-to-echo-in-command-line-input-wont-show-up) – Stephan Nov 15 '19 at 17:10
  • This wouldn't even work in Unix. You redirect stdout into con and then again redirect it into log.txt. It probably is a duplicate question as Stephan said. – Gunther Schadow Nov 15 '19 at 17:19

3 Answers3

0

If you are on a supported Windows system, it will have PowerShell.

& tasklist | Tee-Object -FilePath .\tee.txt | Out-File -FilePath .\tee2.txt

This can be done in a cmd.exe shell using:

powershell -NoLogo -NoProfile -Command "& tasklist | Tee-Object -FilePath .\tee.txt | Out-File -FilePath .\tee2.txt"

This will produce tee.txt and tee2.txt as Unicode encoded files. If you want them as ASCII files, they will need to be converted.

Get-Content -Path .\tee.txt | OutFile -FilePath .\tee3.txt -Encoding ascii
Get-Content -Path .\tee2.txt | OutFile -FilePath .\tee4.txt -Encoding ascii
lit
  • 14,456
  • 10
  • 65
  • 119
0

Writes Stdin to a file and StdOut

Put into a file called tee.vbs

Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
Set Fso = CreateObject("Scripting.FileSystemObject")
Set File = Fso.CreateTextFile(WScript.Arguments(0), True)
If err.number <> 0 then
    Outp.WriteLine "Error: " & err.number & " " & err.description & " from " & err.source
    err.clear
    wscript.exit
End If
Do Until Inp.AtEndOfStream
    Line=Inp.readline
    outp.writeline Line
    File.WriteLine Line
Loop

To use

cscript //nologo tee.vbs "%userprofile%\Desktop\winini.txt" < "%windir%\win.ini"
Mark
  • 61
  • 3
0

I have created a tool to emulate tee in batch file:

@ECHO OFF
SETLOCAL
 SET Append=false 
IF /I "%~1]=="/a" ( SET Append=true & SHIFT )
IF "%~1"== "" GOTO help
IF "%~2"== "" GOTO help
 SET Counter=0 
FOR /F %%A IN ('DIR /A /B %1 2^>NUL') DO CALL :Count "%%~fA"
 IF %Counter% GTR 1 ( SET Counter= & GOTO Syntax ) 
 SET File=%1
 DIR /AD %File% >NUL 2>NUL
 IF NOT ERRORLEVEL 1 ( SET File= & GOTO Syntax ) 
 SET Y= 
VER | FIND "Windows NT" > NUL 
IF ERRORLEVEL 1 SET Y=/Y
 IF %Append%==false  (COPY %Y% NUL %File% > NUL 2>&1) 
 FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
    > CON ECHO.%%B  
        >> %File% ECHO.%%B
 ) 
ENDLOCAL 
GOTO:EOF
 :Count 
SET /A Counter += 1
 SET File=%1 
GOTO:EOF
 :help
 ECHO.
 ECHO Display text on screen and redirect it to a file simultaneously ECHO Usage: some_command ^| TEE.BAT [ /a ] filename 
 ECHO. 
ECHO Where: "some_command" is the command whose output should be redirected
 ECHO "filename" is the file the output should be redirected to
 ECHO /a appends the output of the command to the file,
 ECHO rather than overwriting the file
 ECHO. 
ECHO Made by Wasif Hasan.

Usage: command | tee [/a] filename. /a switch append input to file rather than overwriting it.

Wasif
  • 14,755
  • 3
  • 14
  • 34