Click here to Skip to main content
15,867,330 members
Articles / Operating Systems / Windows
Tip/Trick

Automatic Copying of Release Executables to a Specific Folder on a Successful Build

Rate me:
Please Sign up or sign in to vote.
4.76/5 (5 votes)
7 Sep 2020CPOL2 min read 13.7K   8   7
How to automatically copy release executables to a specific folder on a successful build
I have a couple of apps that I use on my desktop and my Surface - and it's a pain when I change them, as I always forget to update the Surface to run the latest EXE. So I figured I'd add a post-build step to Visual Studio to automate a copy of Release (and Release only) files (the EXE and all its related / supporting DLLs) to my NAS for easy access. Turns out it's not that trivial.

Background

After much Googling, and a good deal of experimentation, I ended up with this.

Start by adding a post-build step:

Open your solution and then the Properties page for your "startup project".

Select the Build Events tab on the left and copy this string to the "post-build event command line":

if "$(ConfigurationName)" == "Release" 
( xcopy "$(SolutionDir)$(TargetName)\bin\$(Configuration)" "S:\My Utils\MediaMaster\" /s /i /y )

Save the properties, and build a Release build.

What the string Does

if <condition> ( <command> )

The brackets allow for multiple commands: if you use them, put them on separate lines:

if <condition> ( 
command1
command2
...
commandn
)

The condition could be simplified:

"$(ConfigurationName)" == "Release"

The double quotes aren't technically needed, but ... if you don't have them and your configuration contains a space, it will all fall apart with a syntax error. Leave 'em there: you know it makes sense!

$(...) is a Visual Studio macro : Macro names

$(ConfigurationName) is the name of the config you have selected to build: VS automatically generates "Debug" and "Release" for you when you create a project, but you can change the names or add custom configs if you need to. In this case, I'm only interested in Release builds as I don't want Debug EXE files to mess up apps on the Surface.

The brackets '(' and ')' delimit a list of DOS / Shell commands you want to run: again, they aren't necessary for a single command (provided it's on the same line as the condition) but again, it costs nothing to add them and may make your life easier later.

The command itself is a PITA: I went through several iterations of Google provided "close but gives Error 4" solutions before I ended up with:

xcopy "$(SolutionDir)$(TargetName)\bin\$(Configuration)" "S:\My Utils\MediaMaster\" /s /i /y

xcopy is a DOS / Shell file copy utility which copies files, directories, and subdirectories: XCopy (MS Docs)

Its parameters come in three parts.

The Source File(s) are First

"$(SolutionDir)$(TargetName)\bin\$(Configuration)"

$(SolutionDir) is the root folder for the project. In my case "D:\Documents\AA Backed Up\My Projects\MediaMaster".

$(TargetName) is the name of the startup project. In my case "MediaMaster".

\bin\ is the folder VS keeps compiler outputs in.

$(Configuration) isn't listed in the MS documentation, but appears to be the same as $(ConfigurationName). It works. I'm not changing it to find out.

Then the Destination

"S:\My Utils\MediaMaster\"

Just the folder name I want all my release files copied to.

Finally the XCopy switches

/s /i /y

/s specifies it should copy directories and subdirectories.

/i creates new folders as required.

/y overwrites existing files without prompting.

History

  • 8th September, 2020: First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
Praisethis is great Pin
Southmountain1-Oct-21 15:46
Southmountain1-Oct-21 15:46 
Questionerror Pin
Александр Куприянов28-Sep-21 22:35
Александр Куприянов28-Sep-21 22:35 
AnswerRe: error Pin
OriginalGriff28-Sep-21 23:51
mveOriginalGriff28-Sep-21 23:51 
AnswerRe: error Pin
Nirav Savla20-Sep-23 20:35
Nirav Savla20-Sep-23 20:35 
QuestionWhat is here new comparing to VC++6.0? Pin
Victor Nijegorodov3-Oct-20 3:02
Victor Nijegorodov3-Oct-20 3:02 
GeneralMy vote of 5 Pin
Rusty Bullet9-Sep-20 5:20
Rusty Bullet9-Sep-20 5:20 
GeneralMy vote of 5 Pin
gugy19-Sep-20 1:52
gugy19-Sep-20 1:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.