2

What is the best way to copy and always overwrite a file to the target directory in a postbuild event in VS2010 running on windows 7.

At the moment I am using

robocopy $(SolutionDir) $(TargetDir) "Morning Report Template.xlsm"

I have also tried using Xcopy (with /Y) and even just plain copy. But I have not made it work properly yet. Either I get build errors like "The command "robocopy C:\Working\Projects\SAFEXQueryForm\ C:\Working\Projects\SAFEXQueryForm\SAFEXQueryForm\bin\Release\ "Morning Report Template.xlsm"" exited with code 1." Or else it just doesn't copy.

I need it to copy and overwrite everytime, without build errors and I would also prefer to change the file name which I know Robocopy can't do.

What am I doing wrong? And what is the best way to do this?

7
  • 1
    copy /Y "$(SolutionDir)\Morning Report Template.xlsm" $(TargetDir) ? Commented Apr 13, 2012 at 13:07
  • Does nothing... Is there some other setting somewhere? Commented Apr 13, 2012 at 13:30
  • @Tom QUarendon: If I clean the build and delete the file then that code copies the file. If I delete the file and just build however, then it doesn't copy. How can I get it to execute always? Commented Apr 13, 2012 at 13:41
  • Ah, I think I see. If I understand correctly the problem is nothing to do with the COPY action, rather to do with getting visual studio to actually do the copy every time you select "build" on the project? That is, presumably if you do what you describe, you get a message saying that the project is up to date 00 it doesn't actually run the build at all, so the "post build event" doesn't get run at all. Commented Apr 13, 2012 at 14:17
  • Yeah the copy action always works from the command prompt. So, you are saying that if press F5 or click the green play button, that a post build event won't necessarily be called? Why not? And are there alternatives that you know of? Also it doesn't explain why robocopy gives me the error... Commented Apr 13, 2012 at 14:41

2 Answers 2

7

EDIT 2015/11/23

This answer provides a better method: https://stackoverflow.com/a/4596552/1011724. You can add the file to the project and then change the "Copy to Output Directory" property of the file.


Original answer

I still don't know what was wrong with my original syntax or how to convince VS that Robocopy's success exit code is 1 but this is what I have now and it seems to work, the only difference being that I changes the directory structure but that shouldn't matter(I'm afraid I don't know if I made other changes in the interim, this was quite a while ago)

xcopy "$(SolutionDir)\Additional Files\Morning Report Template.xlsm" "$(TargetDir)" /Y

and also I have the Run post build event drop down set to On successful build

Sign up to request clarification or add additional context in comments.

Comments

1

You need to use a custom build action to achieve this. See http://msdn.microsoft.com/en-US/library/hefydhhy(v=vs.80).aspx for details, but here's what I tried.

I added the input file to the project. Then select the file and show the properties page (right click -> properties). On the General page make sure that the "Item Type" is "Custom Build Tool".

You may need to close and reopen the properties dialog, but having changed "Item Type" to "Custom Build Tool" there should be a "Custom Build Tool" page in the properties dialog. You can then fill in the command line. Make sure that you fill in the "Outputs" section with the name of the file our custom build step generates.

You should then find that the project builds and runs the custom build step whenever it finds that the input file has a date greater than the output file, which I believe is what you are trying to achieve.

9 Comments

That sounds like what I want, I'll give this a try Monday morning. Thanks a lot!
I'd like to give this a try but I can't seem to find the properties page for a file. (nor the configuration box mentioned in the link). I right clicked on the project and chose 'add existing item', selected the file (a .xlsm). Then I right clicked on the file in the solution explorer and chose properties but all I get is the normal properties explorer like the one you get for controls in the win form designer. Can't find an 'item type'. There is a Build Action drop down, and a Custom Tool text field though. Where have I gone wrong?
In the properties dialog for a normal file, on the left I have a tree consisting of "Configuration" containing "General". Both "Configuration" and "General" link to the same property page on the right. It contains a setting "Exclude from build", which by default is "no", and a property "Item type" which by default is "Does not participate in build". What project type do you have? I'm working with Visual C++ projects.
Visual C#, unless I'm looking in the wrong place, I have no "Configuration" nor "General". But also - the robocopy code above is actually working, but for some reason VS is seeing the exit code of 1 (which is robocopy for success) as an error code, so maybe a custom build event is not necessary after all? Just need to find a way to convince VS that robocopy exiting with 1 is not an error... which I have no idea how to do :/
You can certainly make Visual studio do the copying for you, and if you do it will ensure it only needs to be copied if necessary (i.e if out of date).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.