13

I want to create a Visual Studio (I am using VSTS 2008) project which simply does file copy work, in more details, I will add some files to this project and this project copy files (included in this project) to some destination location when I build the project.

Any ideas how to do this in VSTS?

BTW: I heard using proj file could do such kinds of task, but I have not found any good simple to learn samples for beginner. :-)

thanks in advance, George

Lucero
  • 59,176
  • 9
  • 122
  • 152
George2
  • 44,761
  • 110
  • 317
  • 455

6 Answers6

13

You can add a post build step in Visual Studio:

  • Open the project properties (this works in C# projects, I guess for VB.NET this applies as well).
  • Select the Build Events tab
  • There you can add the post build event commands, for example

    copy $(ProjectDir)\bin\* SOME_OTHER_FOLDER\*

The $(ProjectDir) is a macro, there are a few more available, they will be shown, when you edit a command.

Then, if you have a look at the according project file (XYZ.csproj or XYZ.vbproj), you can see a property group added at the bottom of the file, such as:

  <PropertyGroup>
      <PostBuildEvent>copy $(ProjectDir)\bin\* SOME_OTHER_FOLDER\*</PostBuildEvent>
  </PropertyGroup>

This is how you would do it when directly editing an MSBuild file. Note that you don't need to launch Visual Studio in order to build and have your files copied, you can just pass the project file to the msbuild.exe.

kay.herzam
  • 3,053
  • 3
  • 26
  • 37
  • 1
    I would use xcopy which is a little more fail-safe. – KJAWolf Jul 03 '09 at 17:41
  • 1
    PostBuildEvent is not a "great" solution as it does not check dependencies, that is if one the files being copied changed, the post build will not detect it and will not be rerun. – okigan Nov 15 '11 at 03:48
  • 2
    use `Copy /Y source destination` if you need to copy the files, even if they will be overwritten – northben May 06 '13 at 21:56
  • okigan, how about you providing the "great" solution? Perhaps you don't know, but I'm eager to know if you do have any more information =) – Martin Andersson Sep 04 '13 at 14:26
  • 1
    I googled around and found "robocopy c:\source c:\target /mir" useful. It will not only copy but actually mirror the source directory. The /mir flag can thus have the effect of deleting files from target that was not found in source. See [this link](http://en.wikipedia.org/wiki/Robocopy). – Martin Andersson Sep 04 '13 at 14:36
5

As mentioned before, Visual Studio .xxproj files are actually MSBuild files. So you can do in them whatever MSBuild allows them to do. I've used them to customize my build process quite a bit. In your case, what you're looking for is the Copy Task. You can add it in the AfterBuild target.

Vilx-
  • 104,512
  • 87
  • 279
  • 422
  • This was my first thought as well, but he asked about doing it in VS(TS). – kay.herzam May 04 '09 at 14:16
  • 1
    Yes, so? Doesn't this do what he wanted? When the project is built, the files in it are copied to some location. There are even great many variables he can play with. – Vilx- May 04 '09 at 17:59
0

The other answers to this question tend to assume you are not using VisualStudio but MSBuild directly or have an existing project for which you can add a custom build step or a build event (I have found build event type of solutions error prone because they will not always copy the files when you need them copied).

If you want a VisualStudio project that only does a copy, you can do this with a stock VisualStudio with a Makefile project.

The slightly misnamed Makefile project can do anything you can do from the command line. Obviously this means you can create a Makefile project that only copies files. In a Makefile project's PropertiesConfiguration PropertiesNMake section are fields for Build, Rebuild, and Clean actions. These can be populated with plain old batch file commands. On every build, VisualStudio will create a temporary batch file from the proper field and execute it with cmd. Warning you will get the 32-bit cmd interpreter on 64-bit machines so you will need to use the magical sysnative directory if you want access 64-bit windows commands or DLLs (I'm not sure if this is documented anywhere so it may not always be the case - your batch scripts should be robust against the bit-ness of cmd changing.

Example batch code requiring 64-bit system files without knowing beforehand what cmd or OS (32-bit or 64-bit) will run the code:

if /i "%programfiles%"=="%programfiles(x86)%" if /i not "%programfiles%"=="%programw6432%" (
    REM 32 bit cmd on 64-bit computer
    REM Use the 'sysnative' fake directory to get to 64-bit system32 files
) else (
    if /i not "%programfiles%"=="%programfiles(x86)%" if /i "%programfiles%"=="%programw6432%" (
        REM 64 bit cmd on 64-bit computer
        REM Use the 'system32' directory to get to 64-bit system32 files
    ) else (
        echo "Cannot do 64-bit on 32-bit computer"
        copy foobar
    )
)

(thie "copy foobar" line above will fail causing the build to fail.)

Note, VisualStudio (and MSBuild) will always run a Makefile project - it does not check the timestamp on the output file but instead punts that job to what it expects to be some other build tool (nmake, cmake, make, ant, maven, etc.). Since, in this case there is just a simple copy command and not some program checking whether or not to perform the copy, the copy occurs on every build.

VisualStudio always running makefile projects also means that every time you press F5 to debug your project, VisualStudio will annoy you with a poorly worded pop up a dialog box (if you haven't told it not to) telling you that you your project is out of date and that you must build it again. If you tell VisualStudio not to display the dialog, it will just perform the copy without asking before running the executable.

mheyman
  • 4,211
  • 37
  • 34
0

In Visual Studio 2010, you use Custom Build tools

For example, this rule copies "faq.txt" to the output directory.

<ItemGroup>
  <CustomBuild Include="faq.txt">
    <Message>Copying readme...</Message>
    <Command>copy %(Identity) $(OutDir)%(Identity)</Command>
    <Outputs>$(OutDir)%(Identity)</Outputs>
  </CustomBuild>
</ItemGroup>
bobobobo
  • 64,917
  • 62
  • 258
  • 363
0

For a less powerful but more native solution:

In Visual Studio, Open up project 'Configuration Properties'->'Advanced' and set 'copy Content to outDir' to 'yes'. Assuming the file you wish to copy is part of your solution, open the properties of the file you wish to copy, and set 'Content' to 'yes'. Remember to do this for both release and debug configurations.

Now your file will be copied to the output directory on compilation. Useful for copying asset files like configuration or graphics.

(I was using visual studio 2022, it may not work in earlier versions)

0

The C#/VB.NET project files are MSBuild scripts. MSBuild can do whatever you need...

Lucero
  • 59,176
  • 9
  • 122
  • 152
  • Thats true, but the question is how is the requirement in the question acheived? – AnthonyWJones May 04 '09 at 13:35
  • I know MSBuild. I want to find some ready to use/reference sample to do file copy. Any recommendations? – George2 May 04 '09 at 13:35
  • Have a look at this file (MSI, but installs samples only) with MSBuild samples and word documentation for them: http://download.microsoft.com/download/d/4/0/d401e5cd-04c7-4b72-b644-a00ae3fd5681/msbuildqs.msi – Lucero May 04 '09 at 13:37
  • Installed and find a couple of good samples, but not find an ready to use file copy sample. Could you recommend me a sample to start quickly to solve my confusion? :-) – George2 May 04 '09 at 14:02
  • 1
    Here's a simple sample: http://www.sedodream.com/PermaLink,guid,8f6bbb30-ec22-4fc6-90a1-c32715598d23.aspx – Lucero May 04 '09 at 14:06