还有另一种解决方案,它也是基于识别发送到 STDERR 的错误消息,因此它与 dbenham 在他的回答中描述的问题相同,但它不需要以任何方式修改现有的批处理文件。
该解决方案使用this trick 以一种颜色显示以数字开头的正常行和以另一种颜色开头的错误行。这样,您可以区分屏幕中的错误行,尽管如果输出被重定向,颜色不会存储在文本文件中。
( theBatchFile | findstr /N /A:2A "^" ) 2>&1 1>&3 | findstr /N /A:4E "^"
这种方法的问题是正常行和错误行不是按照原来的顺序出现,而是在分隔的部分。发生这种情况是因为每个行块都由不同的findstr 命令处理,但这种解决方案在某些情况下可能就足够了。
编辑:添加了新方法
我对此解决方案进行了多次测试。 Stdout 和 Stderr 线并没有像我最初认为的那样分组。它的顺序取决于启动第二个findstr 的初始延迟,这导致初始 Stderr 行被延迟,以及两个输出可能在屏幕中混合的事实。如果批处理文件不时显示大多数 Stdout 行和少数 Stderr 行,则输出将保留原始顺序。
我编写了一个名为 ShowErrors.bat 的小型 Batch-JScript 混合脚本,它允许以您希望的任何方式区分 Stderr 行:
@if (@CodeSection == @Batch) @then
@echo off
if "%~1" equ "" (
echo ShowErrors.bat command parameters ...
echo/
echo Execute the command and differentiate lines sent to Stderr
goto :EOF
)
%* 2>&1 1>&3 | CScript //nologo //E:JScript "%~F0"
goto :EOF
@end
while ( ! WScript.Stdin.AtEndOfStream ) {
WScript.Stdout.WriteLine("======================================================");
WScript.Stdout.WriteLine("ERROR: "+WScript.Stdin.ReadLine());
}
这是一个小批处理文件,作为混合 Stdout/Stderr 输出程序的示例:
@echo off
rem Initial delay
ping -n 2 localhost > NUL
for /L %%i in (1,1,4) do echo Starting lines to Stdout
for %%a in ("2 4" "3 6" "4 8") do (
for /F "tokens=1,2" %%i in (%%a) do (
echo %%i lines to Stderr: >&2
for /L %%x in (1,1,%%i) do echo Line %%x to Stderr >&2
ping -n 1 localhost > NUL
echo %%j lines to Stdout:
ping -n 1 localhost > NUL
for /L %%x in (1,1,%%j) do echo Line %%x to Stdout
)
)
这是正常执行时的输出:test:
Starting lines to Stdout
Starting lines to Stdout
Starting lines to Stdout
Starting lines to Stdout
2 lines to Stderr:
Line 1 to Stderr
Line 2 to Stderr
4 lines to Stdout:
Line 1 to Stdout
Line 2 to Stdout
Line 3 to Stdout
Line 4 to Stdout
3 lines to Stderr:
Line 1 to Stderr
Line 2 to Stderr
Line 3 to Stderr
6 lines to Stdout:
Line 1 to Stdout
Line 2 to Stdout
Line 3 to Stdout
Line 4 to Stdout
Line 5 to Stdout
Line 6 to Stdout
4 lines to Stderr:
Line 1 to Stderr
Line 2 to Stderr
Line 3 to Stderr
Line 4 to Stderr
8 lines to Stdout:
Line 1 to Stdout
Line 2 to Stdout
Line 3 to Stdout
Line 4 to Stdout
Line 5 to Stdout
Line 6 to Stdout
Line 7 to Stdout
Line 8 to Stdout
这是通过ShowErrors test执行时的输出:
Starting lines to Stdout
Starting lines to Stdout
Starting lines to Stdout
Starting lines to Stdout
======================================================
ERROR: 2 lines to Stderr:
======================================================
ERROR: Line 1 to Stderr
======================================================
ERROR: Line 2 to Stderr
4 lines to Stdout:
Line 1 to Stdout
Line 2 to Stdout
Line 3 to Stdout
Line 4 to Stdout
======================================================
ERROR: 3 lines to Stderr:
======================================================
ERROR: Line 1 to Stderr
======================================================
ERROR: Line 2 to Stderr
======================================================
ERROR: Line 3 to Stderr
6 lines to Stdout:
Line 1 to Stdout
Line 2 to Stdout
Line 3 to Stdout
Line 4 to Stdout
Line 5 to Stdout
Line 6 to Stdout
======================================================
ERROR: 4 lines to Stderr:
======================================================
ERROR: Line 1 to Stderr
======================================================
ERROR: Line 2 to Stderr
======================================================
ERROR: Line 3 to Stderr
======================================================
ERROR: Line 4 to Stderr
8 lines to Stdout:
Line 1 to Stdout
Line 2 to Stdout
Line 3 to Stdout
Line 4 to Stdout
Line 5 to Stdout
Line 6 to Stdout
Line 7 to Stdout
Line 8 to Stdout