Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to Capture Error Messages from a Batch file

3,684 views
Skip to first unread message

rpb...@gmail.com

unread,
Oct 10, 2015, 7:28:08 PM10/10/15
to
Hello! I am thinking this is an easy task, but unfortunately I haven't been able to find the best solution yet.

Here's a simple model of the problem:

With two batch files . . .

D:\Testing\batch1.bat is a simple directory call in DOS. Directory C: is the only directory that exists. So drive X and Y will produce errors.

Echo off
cls
dir C:
dir X:
dir Y:


batch2.bat simply executes batch1 and directs its output to a text file called batch1LOG.txt in directory D:\Testing as follows:

D:\testing\batch1.bat > D:\Testing\batch1LOG.txt

This is what the contents of batch1LOG.txt looks like:

D:\testing>echo off
Volume in drive C has no label.
Volume Serial Number is 9AE3-249A

Contents of batch1LOG.txt:

Directory of C:\

10/03/2015 03:08 PM <DIR> $SysReset
09/06/2015 11:38 PM <DIR> .jagex_cache_32
06/19/2015 11:17 PM <DIR> HTM Import
08/23/2015 10:28 AM <DIR> ICONS
05/06/2015 01:36 PM <DIR> Intel
05/11/2015 12:58 AM <DIR> MSI
08/22/2013 10:22 AM <DIR> PerfLogs
10/03/2015 10:10 PM <DIR> Program Files
10/06/2015 09:44 PM <DIR> Program Files (x86)
10/03/2015 10:04 PM <DIR> temp
10/03/2015 08:11 PM <DIR> Users
10/06/2015 09:28 PM <DIR> Windows
5 File(s) 33,379 bytes
12 Dir(s) 81,159,696,384 bytes free



Here's the problem: If you run batch1.bat from a Command Window it produces the following output -- including the errors.

Contents of batch1LOG.txt:

Directory of C:\

10/03/2015 03:08 PM <DIR> $SysReset
09/06/2015 11:38 PM <DIR> .jagex_cache_32
06/19/2015 11:17 PM <DIR> HTM Import
08/23/2015 10:28 AM <DIR> ICONS
05/06/2015 01:36 PM <DIR> Intel
05/11/2015 12:58 AM <DIR> MSI
08/22/2013 10:22 AM <DIR> PerfLogs
10/03/2015 10:10 PM <DIR> Program Files
10/06/2015 09:44 PM <DIR> Program Files (x86)
10/03/2015 10:04 PM <DIR> temp
10/03/2015 08:11 PM <DIR> Users
10/06/2015 09:28 PM <DIR> Windows
5 File(s) 33,379 bytes
12 Dir(s) 81,159,696,384 bytes free
The system cannot find the path specified.
The system cannot find the path specified.


My question -- (if there is a switch or a verbose setting): What needs to be added to batch2.bat so that it includes the errors.

Thanks in advance!

RBollinger




















foxidrive

unread,
Oct 11, 2015, 1:14:09 AM10/11/15
to
On 11/10/2015 10:28, rpb...@gmail.com wrote:
> Here's the problem: If you run batch1.bat from a Command Window it produces the following output -- including the errors.

Google on how to redirect STDERR (standard error).

frank.w...@gmail.com

unread,
Oct 11, 2015, 4:17:02 PM10/11/15
to
From rpboll:
>D:\testing\batch1.bat > D:\Testing\batch1LOG.txt

The redirection operator '>' is more completely spelled
'1>'. The numeral is the file number of the stream:

0=standard input stream;
1=standard output stream;
2=standard error stream.

If you want 1 and 2 to go to the same file then redirect
one to the other then the other to the file (paths
trimmed for clarity):

batch1.bat 2>&1 1> batch1LOG.txt

Since 'batch1.bat' is a script the current command
interpreter is transferred to it and does not return. If
it were a command, COM, or EXE it would return. If you
want to return from the script then use CALL:

CALL batch1.bat 2>&1 1> batch1LOG.txt

Frank

foxidrive

unread,
Oct 11, 2015, 5:10:07 PM10/11/15
to
On 12/10/2015 02:02, frank.w...@gmail.com wrote:
> If you want 1 and 2 to go to the same file then redirect
> one to the other then the other to the file (paths
> trimmed for clarity):
>
> batch1.bat 2>&1 1> batch1LOG.txt

Frank, assuming all streams have not yet been changed
then STDOUT redirection should be set first and then STDERR

batch1.bat 1>batch1LOG.txt 2>&1

In your case above STDERR will still go to the console because at the time
it was changed, STDOUT was already going to the console.



rpb...@gmail.com

unread,
Oct 13, 2015, 9:23:45 AM10/13/15
to
This: batch1.bat 1>batch1LOG.txt 2>&1 works like a champ!

Thank you both! You just made me a crib-sheet genius!

vinod...@gmail.com

unread,
Feb 3, 2016, 1:41:00 AM2/3/16
to
Thanks for your suggestions, This will help full for me
0 new messages