batch-file - 批处理文件 - 错误代码

标签 batch-file msbuild

我在 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/

相关文章:

batch-file - 测试批处理文件中的文件属性

batch-file - 用于将文件(文本文件中的输入行)从一个文件夹复制到另一个文件夹的批处理文件脚本

visual-studio - 无需安装即可使用 msbuild 社区任务

azure - Azure 中的 Visual Studio Team Services 构建服务器

linux - 如何将cygdrive更改为windows目录结构

batch-file - 如何使用cmd从文本文件中删除第一行?

visual-studio-2008 - VS 2008 中的解决方案级构建事件

c# - 无法使 StyleCop 错误显示为构建错误(而不是警告)

windows - 使用 SFTP 将文件从 Windows 传输到 Linux

msbuild - 从命令行编译时添加附加库并包含路径