3

I write a batch file. does it possible to send the output to log file and also to screen?? my command is: sqlplus -S %USER_NAME%/%PASSWORD%@%TNS_NAME% OG.2ation.sql I want that the output of this command will be in the screen and also to log file.????

double-beep
  • 5,031
  • 17
  • 33
  • 41
zipi
  • 633
  • 5
  • 12
  • 20
  • If you are on *nix then the `tee` command will do it. As in `sqlplus -S ... | tee mylog.log`. – user1666959 Sep 20 '12 at 06:04
  • 1
    possible duplicate of [Displaying Windows command prompt output and redirecting it to a file](http://stackoverflow.com/questions/796476/displaying-windows-command-prompt-output-and-redirecting-it-to-a-file) and of [Displaying and storing Windows Batch file output](http://superuser.com/questions/29295/displaying-and-storing-windows-batch-file-output). – Eitan T Sep 20 '12 at 07:44
  • 1
    Another duplicate - this one with a simple hybrid batch/JScript answer that provides a proper asynchronous tee functionality using purely native scripting: [Using a custom Tee command for .bat file](http://stackoverflow.com/q/10711839/1012053) – dbenham Sep 20 '12 at 11:56

1 Answers1

4

You could write your own tee program with batch.

Use it with myProgram | tee.bat outfile.txt

@echo off
setlocal DisableDelayedExpansion
set "outfile=%~1"

(
  for /F "usebackq delims=" %%L in (`find /N /V ""`) DO (
    set "line=%%L"
    setlocal EnableDelayedExpansion
    set "line=!line:*]=!"
    (echo(!line!) > con
    (echo(!line!)
    endlocal
  )
) > "%outfile%" 
jeb
  • 78,592
  • 17
  • 171
  • 225
  • Nice, but unfortunately this script runs into problems if `myProgram` reads from `stdin`. For instance, if `myProgram` is a batch script with 3 lines: `@echo Hello`, `@pause` and `@echo world`, then nothing is displayed on the console until `myProgram` terminates... – Eitan T Sep 20 '12 at 08:16
  • Yes, it waits until the complete data is transfered, but in the case of the OP this seems to be OK – jeb Sep 20 '12 at 08:18
  • 1
    Wouldn't `myProgram >outfile.txt & type outfile.txt` work just as effectively as this batch script? It sure is a lot simpler. – dbenham Sep 20 '12 at 12:07
  • @dbenham - Hmmm, perhaps you are right :-) But with tee.bat you could change the code of tee.bat to an async variation – jeb Sep 20 '12 at 12:34