返回列表 发帖

[其他] [已解决]批处理使用管道后,如何捕获errorlevel?

本帖最后由 shootman2 于 2015-3-27 01:02 编辑

假如使用了管道符后,再去捕获errorlevel时发现,其值已变为0,即便之前的命令是否返回0。
如下代码

call myCommand.bat|mtee /d /t /+ sysLog.log

如果调用myCommand.bat后返回errorlevel值为1,但是执行如上代码后,errorlevel值又变为0了,
求大神帮助!!!
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

回复 2# bailong360

我试过了,不行的!

TOP

回复 4# bailong360


    还是没有达到我想要的结果,errorlevel是获取正确了,但是没有正确的将打印结果输出到sysLog.log文件中

TOP

管道中不允许有独立的内部命令或代码块,所以这个 call myCommand.bat|mtee /d /t /+ sysLog.log 相当于而只 ...
CrLf 发表于 2015-3-24 22:15



    CrLf 大神,太牛了!32个赞给你。但是能不能稍微简化一下,不好理解呀!

TOP

回复  bailong360


回复  shootman2


仔细想一下其实不需要交换句柄 1 和 2,化简一下:(外交部 ...
CrLf 发表于 2015-3-24 23:48



    myCommand.bat是有输出的,当我执行您写的代码后,结果是这样的。。。
1
call myCommand.bat 的退出码是 2015-03-25 12:06:11.833 1231312312
call myCommand.bat 的退出码是 2015-03-25 12:06:11.837 12312312312312312
请按任意键继续. . .

    myCommand.bat内容如下
@echo off
echo 1231312312
echo 12312312312312312
exit /b 1

TOP

回复 11# CrLf


    哈哈!实现了,感谢您,对您的景仰有如滔滔江水!

  不过还有个问题,当myCommand.bat有报错的话,是直接打印出来的,您的代码,就直接给屏蔽掉了。
  能帮忙再改改嘛?

TOP

回复 13# CrLf


    嗯!正解。。。大神,你太牛了!对您的景仰有如滔滔江水,永远都流不尽啊!

TOP

回复 13# CrLf


@echo off
set exit_code_file=%thd_tmp_folder%\errorlevel.txt
(call myCommand.bat & call echo %%errorlevel^^%% >"!exit_code_file!") |mtee /d /t /+ sysLog.log
set /p error=<"!exit_code_file!"
echo myCommand.bat 的退出码为 %error%
pause

报错了,提示找不到文件,这是咋回事呢?

TOP

回复 17# CrLf

是这样的,您看!我把errorlevel.txt文件扩展了一下,给放到了一个特殊的目录下,文件的完整路径为:
set exit_code_file=%thd_tmp_folder%\errorlevel.txt       &  rem %thd_tmp_folder%是我指定的一个专门存放临时文件的目录
然后,将所有输出内容到errorlevel.txt的地方都修改成了"!exit_code_file!",就成了下面的这个样子了

@echo off
set exit_code_file=%thd_tmp_folder%\errorlevel.txt
(call myCommand.bat & call echo %%errorlevel^^%% >"!exit_code_file!") |mtee /d /t /+ sysLog.log
set /p error=<"!exit_code_file!"
echo myCommand.bat 的退出码为 %error%
pause

但是它报错了,提示找不到文件,然后我改成是如下代码后,就不报错了,
@echo off
set exit_code_file=%thd_tmp_folder%\errorlevel.txt
(call myCommand.bat & call echo %%errorlevel^^%% >"%thd_tmp_folder%\errorlevel.txt") |mtee /d /t /+ sysLog.log
set /p error=<"%thd_tmp_folder%\errorlevel.txt"
echo myCommand.bat 的退出码为 %error%
pause

我想知道这是为什么?  以上的这段代码是处在一个for循环内部的,且都开启了变量延迟。

TOP

回复 19# CrLf


    确实如您所说,测试代码是正常的
@echo off
set exit_code_file=%cd%\kkk\errorlevel.txt
(call myCommand.bat & call echo %%errorlevel^^%% >"%exit_code_file%") |mtee /d /t /+ sysLog.log
set /p error=<"%exit_code_file%"
echo myCommand.bat 的退出码为 %error%
pause

那像前面的那种写法,有没有解决方法呢?

TOP

回复 21# CrLf


    好像还是不行!我已经加到了4个,6个也不行,8个也不行

@echo off
setlocal enabledelayedexpansion
set exit_code_file=%cd%\kkk\errorlevel.txt
(call myCommand.bat & call echo %%errorlevel^^^^%% >"!exit_code_file!") |mtee /d /t /+ sysLog.log
set /p error=<"!exit_code_file!"
echo myCommand.bat 的退出码为 %error%
pause

2015-03-26 20:00:59.649 1231312312
2015-03-26 20:00:59.654 12312312312312312
系统找不到指定的文件。
myCommand.bat 的退出码为
请按任意键继续. . .

再不行,我就放弃了!

TOP

回复 23# CrLf


    对CrLf大哥的这种精神所震撼,感谢您帮我解决问题。非常感谢!!!

TOP

回复 23# CrLf


    大哥,我又遇到了新问题,我想将myCommand.bat中的错误输出也打印到sysLog.log文件中,论坛里有大神告诉我得加个 2>&1 语句,
但是加了之后一直未生效,但是把后面的语句 ^& call echo %%errorlevel^^^^%% ^>"!exit_code_file!" 去掉后,就ok了,我想让
它们共存,该怎么办呢?求帮助!

如下不能生效的代码
@echo off
setlocal enabledelayedexpansion
set exit_code_file=errorlevel.txt
call myCommand.bat 2>&1 ^& call echo %%errorlevel^^^^%% ^>"!exit_code_file!" |mtee /d /t /+ sysLog.log
set /p error=<"!exit_code_file!"
echo myCommand.bat 的退出码为 %error%
pauseCOPY

TOP

回复 26# bailong360


    正解!太感谢大哥了。。。

TOP

返回列表