Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Batch Script: Redirect ALL output to a file

I am running various Java benchmarks and would like to archive the results. I execute the (dacapo) benchmark like this:

C:\VM\jre\bin\java  -jar C:\benchmarks\dacapo-9.12-bach.jar %arg1% > %time::=%

I pass the type of benchmark in over a parameter, thats what %arg1% is.

You can see that I am redirecting the output to a textfile. Unfortunately, the first and last line of the output is still printed in the console and not into the textfile:

===== DaCapo 9.12 luindex starting =====
===== DaCapo 9.12 luindex PASSED in 2000 msec =====

Especially the last line would be important to have in the text file :)

Is there a trick to force this behavior?

like image 813
Nicolas Avatar asked Mar 11 '13 19:03

Nicolas


People also ask

How do I redirect output to a file in Windows?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.

How do I redirect output to a file?

“>>” operator is used for utilizing the command's output to a file, including the output to the file's current contents. “>” operator is used to redirect the command's output to a single file and replace the file's current content.

What does @echo off do in a batch file?

Example# @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.


2 Answers

You must redirect STDOUT and STDERR.

command > logfile 2>&1

STDIN is file descriptor #0, STDOUT is file descriptor #1 and STDERR is file descriptor #2.
Just as "command > file" redirects STDOUT to a file, you may also redirect arbitrary file descriptors to each other. The >& operator redirects between file descriptors. So, 2 >& 1 redirects all STDERR output to STDOUT.

Furthermore, take care to add 2>&1 at the end of the instruction because on Windows, the order of redirection is important as command 2>&1 > logfile will produce an empty file, as Dawid added in the comments.

like image 50
Zach Riggle Avatar answered Oct 18 '22 22:10

Zach Riggle


Add 2>&1 to your command:

 C:\VM\jre\bin\java  -jar C:\benchmarks\dacapo-9.12-bach.jar %arg1% 2>&1 > %time::=% 

This will redirect STDERR to STDOUT which is then redirected into your textfile.

like image 23
Squeezy Avatar answered Oct 18 '22 21:10

Squeezy