39

I'd like to copy several known files to another directory as a part of a post-build event, but I don't want to have lines and lines of "copy [file] [destination] [switches]" in my build event.

If possible, I'd like to list out the files I'd like to copy using a similar format: "copy [file 1] [file 2] [file 3] [etc...] [destination] [switches]". However, Windows doesn't seem to like this type of format. How can I do it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark Carpenter
  • 17,445
  • 22
  • 96
  • 149

3 Answers3

56

You can use 'for' either in a batch file or directly from the command prompt:

for %I in (file1.txt file2.txt file3.txt) do copy %I c:\somedir\

Wildcards are supported in the filelist as well:

for %I in (*.txt *.doc *.html) do copy %I c:\somedir\

For more info, just type for /? from a command prompt, or for a much easier to read help use Start->Help and Support and search for "For". On my XP Pro box, it was item 15 in the full text search results.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 6
    Works like a charm! The only thing is that I have to double up my percent signs when used in a build event (i.e. for %%I in...). Thanks!! – Mark Carpenter May 28 '09 at 21:24
  • 1
    IF the files are in sub folders add `/f` like `for /f %%I in (*.txt *.doc *.html) do copy %I c:\somedir\ ` – Matheus Oct 27 '16 at 23:54
32

XP and Vista replaced xcopy with robocopy, and it will do exactly what you want. The syntax for what you want feels backwards at first, but it does the job:

robocopy source\folder a\dest\folder file1.exe file2.bat file3.dll file4.txt
Kevin
  • 8,353
  • 3
  • 37
  • 33
  • 1
    (older versions at least) of XP don't include robocopy, you need to install if from something like the windows 2003 server resource kit: http://www.microsoft.com/Downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en – Dolphin May 28 '09 at 21:28
  • @Dolphin: good catch; I had it on my XP machines and didn't remember installing it myself, but you're right. – Kevin May 28 '09 at 22:41
  • 2
    Neither XP nor Vista "replaced" xcopy. It's still there. – Timwi Sep 29 '09 at 02:13
  • 1
    @Timwi, poorly phrased on my part. What I meant is that it was replaced as the preferred way to copy multiple files. – Kevin Sep 29 '09 at 04:34
2

Use the <Copy> MSBuild task.

David Schmitt
  • 58,259
  • 26
  • 121
  • 165
  • The question does not suggest .NET is even installed, and that would require developing an MS Build script. It's already cumbersome just to do a command line copy; why add all that on top? – jpmc26 Jun 19 '14 at 20:12
  • the question talks about copying "as a part of a post-build event". using MSBuild was natural for me. – David Schmitt Jun 26 '14 at 07:57
  • @DavidSchmitt I think this is what I want but I'm not sure how I can use this in a build event. Can you show syntax for doing that? – Jonathan Mee Nov 05 '14 at 13:32
  • You insert this directly into the msbuild/csproj file as XML. – David Schmitt Nov 07 '14 at 09:24