Pergunta

I have a post-build event in my C++ Visual Studio 2010 project, which uses command xcopy, but when this xcopy return error code (>0), all build failed too and message "build unsuccessfull", how can i turn of error sensetivity in build events?

Thanks!

Foi útil?

Solução

You can use the Exec task's IgnoreExitCode:

<Target Name="MyAwesomePostBuildTarget" AfterTargets="Build">
  <Exec IgnoreExitCode="true" Command="xcopy etc. etc." />
</Target>

Outras dicas

You can override the failure result of (almost) any batch CMD by appending || exit /b 0 to the end of the command. Example:

del somefile.txt || exit /b 0

In this way batch files work a bit like C. You can do && to conditionally run a command when the previous command succeeds, and || to run a command when the previous command fails.

exit /b 0 tells the CMD processor to exit the script and set the errorlevel to zero (0). Never forget to include the /b switch! Without it, CMD will exit the calling script as well as the current script which is rarely, if ever, the desired behavior.

I use this trick from the Visual Studio IDE, so there's no need to do low-level project hacking. And it fits on one line, which is also convenient from the IDE.

Another useful trick is silencing the command, by the way:

xcopy srcfile destfile 1>nul 2>nul || exit /b 0

1 is stdout, and 2 is stderr. Windows suite of shell programs are notoriously inconsistent with regard to which output they might use, so I generally just pipe both or pipe neither.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top