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?

Dan
  • 45,079
  • 17
  • 88
  • 157
  • 1
    copy /Y "$(SolutionDir)\Morning Report Template.xlsm" $(TargetDir) ? – Tom Quarendon Apr 13 '12 at 13:07
  • Does nothing... Is there some other setting somewhere? – Dan Apr 13 '12 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? – Dan Apr 13 '12 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. – Tom Quarendon Apr 13 '12 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... – Dan Apr 13 '12 at 14:41
  • It looks like the exit code of 1 is actually a success message (http://ss64.com/nt/robocopy-exit.html) but VS is picking it up as an error. Is there a way around this? – Dan Apr 16 '12 at 15:07
  • Possible duplicate of [VS2010 How to include files in project, to copy them to build output directory automatically during build or publish](http://stackoverflow.com/questions/4596508/vs2010-how-to-include-files-in-project-to-copy-them-to-build-output-directory-a) – Dan Nov 23 '15 at 11:41

2 Answers2

6

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

Dan
  • 45,079
  • 17
  • 88
  • 157
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.

Tom Quarendon
  • 5,625
  • 5
  • 23
  • 30
  • That sounds like what I want, I'll give this a try Monday morning. Thanks a lot! – Dan Apr 13 '12 at 15:38
  • 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? – Dan Apr 16 '12 at 09:26
  • 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. – Tom Quarendon Apr 17 '12 at 07:09
  • 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 :/ – Dan Apr 17 '12 at 07:15
  • 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). – Tom Quarendon Apr 17 '12 at 12:47
  • What kind of Visual Studio project do you have? – Tom Quarendon Apr 17 '12 at 12:47
  • Not sure what you mean, it's a winform application... is that what you mean? Copying when out of date is perfect so long as it does it every time I build. – Dan Apr 17 '12 at 13:01
  • If I create a new Windows Forms Application project and add a random file to it and then select the file in the Solution Explorer, then in the "Properties" view (which is docked bottom right for me, not a popup window), there is "Copy to Output Directory" which look promising. Does setting that to "Copy always" or "Copy of newer" do what you want? – Tom Quarendon Apr 17 '12 at 13:38
  • Ya I've played with that and it didn't work. I can't remember why though, but I did try it before I started with the post build events. For now I just always have to build twice, once for the copy to run (and "error") and the second time just to make sure. And it seems to work fine. But it's not as smooth as it should be. I thought maybe there was an easy standard way to do this. – Dan Apr 17 '12 at 13:58