views:

2908

answers:

3

Is it possible to use xcopy to copy files from several directories into one directory using only one xcopy command?

Assuming that I have the directory tree

root\Source\Sub1\Sub2

I want to copy all .xml files from the directory root\Source including sub folder to root\Destination. I don't want to copy the folder structure, just the files.

A: 

You don't need xcopy for that. You can get a listing of all the files you want and perform the copy that way.

For example in windows xp command prompt:

for /f "delims==" %k in ('dir c:\source\*.xml /s /b') do copy "%k" x:\destination\

The /s goes into all subdirectories and the /b lists only the files name and path. Each file inturn is assigned to the %k variable, then the copy command copies the file to the destination. The only trick is making sure the destination is not part of the source.

+2  A: 

As DandDI said, you don't need xcopy. for statement helps much. However, you don't need to state process outcome of dir command as well, this command helps better

for /R c:\source %f in (*.xml) do copy %f x:\destination\

By the way, when you use it from a batch file, you need to add spare % in front of variable %f hence your command line should be;

for /R c:\source %%f in (*.xml) do copy %%f x:\destination\

when you use it within a batch

kokeksibir
A: 

Thanks. That worked fine - just not from msbuild... Trying to get this to work from an exec command in msbuild.

<Exec WorkingDirectory="$(SolutionRoot)" Command="for /f &quot;delims==&quot; %k in ('dir *.targets /s /b') do copy &quot;%k&quot; &quot;$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TeamBuild&quot;" />

Gets the error:

for /f "delims==" %k in ('dir *.targets /s /b') do copy "%k" "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\TeamBuild" k" "C:\Program was unexpected at this time.

Any suggestions?

Oddleif
Had to use %% not % from the msbuild script file.
Oddleif