2
votes

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?

2
copy /Y "$(SolutionDir)\Morning Report Template.xlsm" $(TargetDir) ? - Tom Quarendon
Does nothing... Is there some other setting somewhere? - Dan
@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
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
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

2 Answers

6
votes

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

1
votes

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.