我在 MSBUILD 中定义了一个目标来执行一个批处理文件,该文件由多个(接近 100 个或更多)xcopy 命令组成。
我观察到 MSBUILD 仅在批处理文件中最后一个 xcopy 失败的情况下才会失败。如果任何其他 xcopy 失败而最后一个 xcopy 成功,则 MSBUILD 认为构建成功。
有没有一种方法可以捕获所有错误并在批处理文件末尾返回失败,以便 MSBUILD 甚至可以在批处理文件中的任何位置发生单个故障时失败。
谢谢 什拉万
最佳答案
是的,AWinkle 的解决方案是解决这个问题的正确方法。但是,它要求您在现有 XCOPY 命令的每个之后插入一行!下面的批处理文件允许您按原样保留所有 100 多个命令,并且只需在开头插入一小部分代码:
@echo off
setlocal EnableDelayedExpansion
rem Locate the beginning of XCOPY commands in this Batch file:
for /F "delims=:" %%a in ('findstr /N /B "XCOPY_COMMANDS" "%~F0"') do set skip=%%a
rem Process the XCOPY commands and accumulate ERRORLEVELS from they all
set AccumulatedErrorLevel=0
for /F "usebackq skip=%skip% delims=" %%a in ("%~F0") do (
%%a
set /A AccumulatedErrorLevel+=!errorlevel!
)
rem Return the accumulated value
exit /B %AccumulatedErrorLevel%
rem Place the original XCOPY commands below next line until the end of this file:
XCOPY_COMMANDS
关于batch-file - 批处理文件 - 错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21103055/