Batch file 如何读取批处理文件中的错误输出流?

Batch file 如何读取批处理文件中的错误输出流?,batch-file,cmd,stderr,Batch File,Cmd,Stderr,我有一个启动服务器的批处理脚本。 然后,如果出现错误,该服务器将其日志打印到STDOUT和STDERR 我想将STDOUT和STDERR重定向到tee命令以分割输出。 使用此命令中的tee命令效果很好 然而,为了使我的应用程序可移植,我不能依赖于UnxUtils,所以我需要编写自己的tee命令 我编写了一个批处理脚本,按照(2.codeblock)中的建议读取标准输出 这是可行的,但只适用于重定向的STDOUT,而不适用于STDERR 注意:文件和命令行中都缺少来自STDERR的输出 如何使它同

我有一个启动服务器的批处理脚本。 然后,如果出现错误,该服务器将其日志打印到STDOUT和STDERR

我想将STDOUT和STDERR重定向到tee命令以分割输出。 使用此命令中的
tee
命令效果很好

然而,为了使我的应用程序可移植,我不能依赖于UnxUtils,所以我需要编写自己的tee命令

我编写了一个批处理脚本,按照(2.codeblock)中的建议读取标准输出

这是可行的,但只适用于重定向的STDOUT,而不适用于STDERR

注意:文件和命令行中都缺少来自STDERR的输出

如何使它同时读取两条流

如何重定向输出(%1是服务器,%2是我希望日志转到的文件)

这很好(因为它调用UnxUtils,而不是我的批处理脚本):

如何读取
tee.bat中的输入:

for /F "tokens=*" %%a in ('findstr /n $') do (
    set "line=%%a"
    setlocal EnableDelayedExpansion
    set "line=!line:*:=!"

    echo(!line!

    if %append%==true (
        echo(!line! >> %output%
    ) else (
        echo(!line! > %output%
    )
    endlocal
)

PowerShell有一个
Tee对象
cmdlet。
$Input
变量接收stdin

echo asf 2>&1 | powershell -NoLogo -NoProfile -Command "$Input | Tee-Object -FilePath './tee.txt'"

另请参见:

您的答案在本页上,我知道PowerShell拥有该命令,但我不知道您可以在仍以经典cmd打印输出时使用该命令。非常感谢!
for /F "tokens=*" %%a in ('findstr /n $') do (
    set "line=%%a"
    setlocal EnableDelayedExpansion
    set "line=!line:*:=!"

    echo(!line!

    if %append%==true (
        echo(!line! >> %output%
    ) else (
        echo(!line! > %output%
    )
    endlocal
)
echo asf 2>&1 | powershell -NoLogo -NoProfile -Command "$Input | Tee-Object -FilePath './tee.txt'"