none
output .bat-file to console AND logfile.txt

    Question

  • Hello,

    in my .bat file i have added the following to a command : >> c:\1.txt

    This will log the output to 1.txt, but not to my console/screen anymore.

    What is the command to have the output to the console AND 1.txt?

    Thx

    Wednesday, February 06, 2013 2:16 PM

Answers

  • Hi,

    You are looking to output to the console and standard output simultaneously, more commonly known as "tee".

    In PowerShell you can use the Tee-Object command. There is not an equivalent in cmd.exe unless you use an external tee program, such as tee.exe. If you have tee.exe, you can write:


    command | tee -a c:\1.txt

    (-a means append rather than overwrite file)

    Bill

    Wednesday, February 06, 2013 3:36 PM
    Moderator
  • There isn't one that is intrinsic to cmd.exe.  If you want you can either search for a unix TEE utility port to Windows or use the following hybrid VBS/batch procedure to do that ...

    :: TeeExamp.cmd
     @echo off
    :: Setup
      call :BuildVBS
      
    :: Your command(s) goes here
      dir *.txt | %TEE% > test.txt
       
    :: cleanup & exit
      del %temp%.\T.vbs & set "TEE=" & exit /b
      
     :BuildVBS
      set TEE=cscript //nologo %temp%.\T.vbs
      (echo set con = createobject("scripting.filesystemobject"^)_
       echo                .opentextfile("con:", 2^)
       echo do until wsh.stdin.atendofstream
       echo   s = wsh.stdin.readline
       echo   wsh.echo s : con.writeline s
       echo loop
      ) > %temp%.\T.vbs


    Tom Lavedas


    Wednesday, February 06, 2013 3:50 PM
    Moderator
  • So there is no option in CMD to have output to both??

    No. Cmd.exe does not have any way to tee output natively.

    Bill

    Thursday, February 07, 2013 2:34 PM
    Moderator

All replies

  • Hi,

    You are looking to output to the console and standard output simultaneously, more commonly known as "tee".

    In PowerShell you can use the Tee-Object command. There is not an equivalent in cmd.exe unless you use an external tee program, such as tee.exe. If you have tee.exe, you can write:


    command | tee -a c:\1.txt

    (-a means append rather than overwrite file)

    Bill

    Wednesday, February 06, 2013 3:36 PM
    Moderator
  • There isn't one that is intrinsic to cmd.exe.  If you want you can either search for a unix TEE utility port to Windows or use the following hybrid VBS/batch procedure to do that ...

    :: TeeExamp.cmd
     @echo off
    :: Setup
      call :BuildVBS
      
    :: Your command(s) goes here
      dir *.txt | %TEE% > test.txt
       
    :: cleanup & exit
      del %temp%.\T.vbs & set "TEE=" & exit /b
      
     :BuildVBS
      set TEE=cscript //nologo %temp%.\T.vbs
      (echo set con = createobject("scripting.filesystemobject"^)_
       echo                .opentextfile("con:", 2^)
       echo do until wsh.stdin.atendofstream
       echo   s = wsh.stdin.readline
       echo   wsh.echo s : con.writeline s
       echo loop
      ) > %temp%.\T.vbs


    Tom Lavedas


    Wednesday, February 06, 2013 3:50 PM
    Moderator
  • So there is no option in CMD to have output to both??
    Thursday, February 07, 2013 7:24 AM
  • Just because you don't like the answer doesn't mean that it isn't so.  Two of us have provided pretty much identical answers.  Are you looking for a third opinion ;^))  Sorry to disappoint, but I seriously doubt you're going to get one.


    Tom Lavedas

    Thursday, February 07, 2013 2:06 PM
    Moderator
  • So there is no option in CMD to have output to both??

    No. Cmd.exe does not have any way to tee output natively.

    Bill

    Thursday, February 07, 2013 2:34 PM
    Moderator