Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using robocopy with Visual Studio 2010 Post-build and Pre-build events

Robocopy outputs 1 upon success, unlike most programs that exit with 0 on success. Visual Studio (and MSBUILD) interprets exit code of 1 as an error.

How can Robocopy be used in Visual Studio post- and pre-build events such that its failure and success are correctly identified by the build environment?

Note: this is more or less a repost of this post.

like image 221
Asaf R Avatar asked Mar 29 '11 10:03

Asaf R


People also ask

How do I open a build event in Visual Studio?

Go to the Solution Explorer the right-click the project then select Properties then go to the Build Events tab. Now if you build the project based upon the selection of configuration name , the Web. config file will be updated.

Does robocopy create destination folder?

The most basic use of robocopy is using a source and destination directory with no options. This option will copy all files (excluding subfolders) from C:\src to C:\dst.

What is post build event command line?

Pre/Post build events are useful when we wish to perform some operations before/after a project is built. These operations are nothing but the Shell commands being used from the command line. Think of a scenario where we build our library project and its . dll is saved into the Project/bin/Release directory.

What is post build?

Post build is used to update parts of the configuration after compile time and is typically used by the OEM. The post build parameters are located in a separate memory area which may be replaced by a new configuration that may be downloaded independently of the other parts for the ECU software.


2 Answers

Adding this answer per request. Based on Asaf's solution, and adding skrebbel's comment.

You can simplify the check to:

robocopy <opt> <src> <tgt>
if %errorlevel% leq 1 exit 0 else exit %errorlevel%

As kindly remarked in the comments, you may want to adjust the '1': It depends on what your operation should treat as an error. Have a look at the meaning of the bits that in combination make up the number returned by robocopy:

0×10 Serious error. Robocopy did not copy any files. This is either a usage error or an error due to insufficient access privileges on the source or destination directories.

0×08 Some files or directories could not be copied (copy errors occurred and the retry limit was exceeded). Check these errors further.

0×04 Some Mismatched files or directories were detected. Examine the output log. Housekeeping is probably necessary.

0×02 Some Extra files or directories were detected. Examine the output log. Some housekeeping may be needed.

0×01 One or more files were copied successfully (that is, new files have arrived).

0×00 No errors occurred, and no copying was done. The source and destination directory trees are completely synchronized.

like image 109
mafu Avatar answered Oct 04 '22 15:10

mafu


With <src>, <tgt> being the copy source and target respectfully, and <opt> being robocopy options:

robocopy <opt> <src> <tgt>
set rce=%errorlevel%
if not %rce%==1 exit %rce% else exit 0

For instance, if we want to copy the project target to c:\temp, without retries and with all sub-directories (empty or not), we'd use:

robocopy /R:0 /E $(TargetDir) c:\temp
set rce=%errorlevel%
if not %rce%==1 exit %rce% else exit 0
like image 42
Asaf R Avatar answered Oct 04 '22 15:10

Asaf R