Reputation: 45
I have a target defined in MSBUILD to execute a batch file which consists of multiple (close to a 100 or more) xcopy commands.
I have observed that the MSBUILD only fails in case of the last xcopy failing in the batch files. If any other xcopy fails and the last one succeeds, MSBUILD considers as build succeeded.
Is there a way that we can trap all the errors and return failures in the end of the batch file so that MSBUILD can fail even on a single failure anywhere in the batch file.
Thanks Shravan
Upvotes: 2
Views: 1310
Reputation: 4083
Port the logic in the batch file into the MsBuild target as tasks rather than an exec task.
This will allow your target to build incrementally so that you're no longer arbitrarily copying 100's of unchanged files for every build, and using a tasks yields better output for diagnosing perf issues down the road.
Upvotes: 0
Reputation: 67256
Yes, the solution from AWinkle is the right way to solve this problem. However, it requires that you insert a line after each one of your existent XCOPY commands! The Batch file below allows you to preserve all of your 100+ commands as they are and just insert a small section of code at beginning:
@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
Upvotes: 2
Reputation: 673
The ERRORLEVEL is only being set by the last XCOPY command. You need to record the ERRORLEVEL after each XCOPY
This will try all of the XCOPY commands even if the prior failed:
@ECHO OFF
SET CURRENT_ERROR_LEVEL=0
REM Repeat this for each of your XCOPY commands
xcopy c:\temp\test.* e:\ /Q /Y > nil
SET /A CURRENT_ERROR_LEVEL=%CURRENT_ERROR_LEVEL%+%ERRORLEVEL%
EXIT /B %CURRENT_ERROR_LEVEL%
Upvotes: 1