Author: rmetzger
Subject: Intercepting cmd.exe output
Posted: 17 April 2014 at 7:13pm
Well, I'm not entirely clear what you can and cannot change.
Can you Change File.bat? It sounds like you may be able to change C:\path\file.bat, and if so, then you can define where output is channeled.
Handle Num Description
STDIN 0 Keyboard input
STDOUT 1 Output to the Command Prompt window
STDERR 2 Error output to the Command Prompt window
3-9 are undefined by Windows, but can be defined by an application.
Your 'PutStdErr function' would correspond to STDERR in batch parlance.
An example of this would look something like this in a batch file:
@echo off
copy C:\aPath\File.txt C:\bPath\NewFile.txt 1>>file.log 2>>file.err
This would copy File.txt from C:\aPath to C:\bPath.
Normal (StdOut) messages would be directed to File.log ("1 file copied" or something to that effect.)
Error (StdErr) messages would be directed to File.err ("Access denied" or other error messages that apply.)
The >> characters mean 'Append to the end of the file' (and create if not exist).
Sometimes it is easier to see all output in a single log file. In this case you can Duplicate handles like so:
copy C:\aPath\File.txt C:\bPath\NewFile.txt >>Filelog.txt 2>&1
>> without the number in front implies handle 1 (StdOut). 2>&1 duplicates all handle 2 output (StdErr) to handle 1 (StdOut) which was directed to Filelog.txt. So, in this example Filelog.txt would contain all standard output and error output to the file for your inspection. Thus normal copy and error in copy will be directed to a single file.
see: ( http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true )
and: ( http://ss64.com/nt/syntax-redirection.html )
for some good examples of how Batch Redirection works (arcane as it is).
Of course, the batch file needs to clean up after itself, and writing log files to shared locations is not recommended.
Hope this helps.
Ron Metzger
Subject: Intercepting cmd.exe output
Posted: 17 April 2014 at 7:13pm
![]() I'm trying to troubleshoot an application that is a Java applet running inside the browser. Upon clicking a button, it runs a .bat files which in turns calls cscript.exe to run a VBScript file with some options. The .bat is executed by calling "cmd.exe /c c:\path\file.bat" and this cannot be changed because it's a 3rd-party app and we're not getting much help from their support. |
Well, I'm not entirely clear what you can and cannot change.
Can you Change File.bat? It sounds like you may be able to change C:\path\file.bat, and if so, then you can define where output is channeled.
Handle Num Description
STDIN 0 Keyboard input
STDOUT 1 Output to the Command Prompt window
STDERR 2 Error output to the Command Prompt window
3-9 are undefined by Windows, but can be defined by an application.
Your 'PutStdErr function' would correspond to STDERR in batch parlance.
An example of this would look something like this in a batch file:
@echo off
copy C:\aPath\File.txt C:\bPath\NewFile.txt 1>>file.log 2>>file.err
This would copy File.txt from C:\aPath to C:\bPath.
Normal (StdOut) messages would be directed to File.log ("1 file copied" or something to that effect.)
Error (StdErr) messages would be directed to File.err ("Access denied" or other error messages that apply.)
The >> characters mean 'Append to the end of the file' (and create if not exist).
Sometimes it is easier to see all output in a single log file. In this case you can Duplicate handles like so:
copy C:\aPath\File.txt C:\bPath\NewFile.txt >>Filelog.txt 2>&1
>> without the number in front implies handle 1 (StdOut). 2>&1 duplicates all handle 2 output (StdErr) to handle 1 (StdOut) which was directed to Filelog.txt. So, in this example Filelog.txt would contain all standard output and error output to the file for your inspection. Thus normal copy and error in copy will be directed to a single file.
see: ( http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true )
and: ( http://ss64.com/nt/syntax-redirection.html )
for some good examples of how Batch Redirection works (arcane as it is).
Of course, the batch file needs to clean up after itself, and writing log files to shared locations is not recommended.
Hope this helps.
Ron Metzger