如何获取命令在Windows批处理编程管道的错误水平? [英] How do I get the error level of commands in a pipe in Windows batch programming?

查看:193
本文介绍了如何获取命令在Windows批处理编程管道的错误水平?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

批处理文件在默认情况下返回最后一个命令的错误code。

Batch files return the error code of the last command by default.

是它在某种程度上可能返回前命令的错误code。最值得注意的是,是否有可能在管道返回命令的错误code?

Is it somehow possible to return the error code of a former command. Most notably, is it possible to return the error code of a command in a pipe?

例如,这一行批处理脚本

For example, this one-line batch script

foo.exe

返回的的误差code。但是这一次:

returns the error code of foo. But this one:

foo.exe | tee output.txt

总是返回的 T恤的出口code,它是零。

always returns the exit code of tee, which is zero.

推荐答案

一个解决办法是通过文件作出间接。

One workaround is to make an indirection through a file.

筛选

foo.exe > tmp.txt
set FOOERR=%ERRORLEVEL%
cat tmp.txt
exit %FOOERR%

这篇关于如何获取命令在Windows批处理编程管道的错误水平?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆