找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
楼主: shootman2

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

[复制链接]
 楼主| 发表于 2015-3-26 11:55:03 | 显示全部楼层
回复 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

报错了,提示找不到文件,这是咋回事呢?
发表于 2015-3-26 15:35:59 | 显示全部楼层
回复 16# shootman2


    我这里测试无误
  1. 2015-03-26 15:35:17.879 1231312312
  2. 2015-03-26 15:35:17.879 12312312312312312
  3. myCommand.bat 的退出码为 1
  4. 请按任意键继续. . .
复制代码
 楼主| 发表于 2015-3-26 16:23:24 | 显示全部楼层
回复 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循环内部的,且都开启了变量延迟。
发表于 2015-3-26 16:52:23 | 显示全部楼层
变量延迟会触发 ^ 的转义,^ 的数量得翻倍
 楼主| 发表于 2015-3-26 17:00:28 | 显示全部楼层
回复 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

那像前面的那种写法,有没有解决方法呢?
发表于 2015-3-26 19:42:38 | 显示全部楼层
回复 20# shootman2


    ^ 的数量翻倍
 楼主| 发表于 2015-3-26 20:02:25 | 显示全部楼层
回复 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 的退出码为
请按任意键继续. . .

再不行,我就放弃了!
发表于 2015-3-26 20:54:36 | 显示全部楼层
回复 22# shootman2


    诡异,括号居然会影响解析,我也没搞明白这里的 !exit_code_file! 为什么没有扩展,不过去掉括号就好了,相当于直接传递给 cmd /c 去解析,这样就一定会扩展
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set exit_code_file=errorlevel.txt
  4. call myCommand.bat ^& call echo %%errorlevel^^^^%% ^>"!exit_code_file!" |mtee /d /t /+ sysLog.log
  5. set /p error=<"!exit_code_file!"
  6. echo myCommand.bat 的退出码为 %error%
  7. pause
复制代码

评分

参与人数 1技术 +1 收起 理由
Batcher + 1 乐于助人

查看全部评分

 楼主| 发表于 2015-3-27 01:01:27 | 显示全部楼层
回复 23# CrLf


    对CrLf大哥的这种精神所震撼,感谢您帮我解决问题。非常感谢!!!
 楼主| 发表于 2015-4-6 19:02:09 | 显示全部楼层
回复 23# CrLf


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

如下不能生效的代码

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set exit_code_file=errorlevel.txt
  4. call myCommand.bat 2>&1 ^& call echo %%errorlevel^^^^%% ^>"!exit_code_file!" |mtee /d /t /+ sysLog.log
  5. set /p error=<"!exit_code_file!"
  6. echo myCommand.bat 的退出码为 %error%
  7. pause
复制代码
发表于 2015-4-6 19:32:56 | 显示全部楼层
回复 25# shootman2


    你得把问题说完整啊...
在myCommand.bat的开头加上@echo off 2>&1 3>&1
这样输出就都正常了
 楼主| 发表于 2015-4-6 19:44:07 | 显示全部楼层
回复 26# bailong360


    正解!太感谢大哥了。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-19 11:21 , Processed in 0.020994 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表