Tuesday, January 25, 2011

Saving the output of a windows batch file (.cmd file) to a file

Sometimes it is useful to save the output of a Windows batch file to a file as well as display it to the screen. This is a possible solution:

@echo off
setlocal
set logfile=Log.log
if not "%1" == "/nolog" (
  (
    date /t & time /t
    call %0 /nolog
  ) >> %logfile% 2>&1
  type %logfile%
  endlocal
  exit /b
) 

... your batch file starts here...

It is much easier to achieve this in a PowerShell script by using the Start-Transcript cmdlet. Tee-Object may also be handy, because it can display the output onto the screen as well as writing to a file.

0 comments:

Post a Comment