18

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.

Oddleif
  • 751
  • 3
  • 9
  • 35
  • 3
    Still it would be interesting to see xcopy soultion if possible – Michal Nov 26 '11 at 01:42
  • For a non-DOS command way, try this- http://www.pcworld.com/article/2105149/gather-similar-files-from-multiple-folders-and-copy-them-in-one-simple-step.html – Swastik Padhi Oct 29 '15 at 09:43

4 Answers4

37

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

  • Should surround %f with double quotes otherwise it will fail copying file names with spaces
APIJunkie
  • 13
  • 3
kokeksibir
  • 1,725
  • 2
  • 18
  • 30
  • is the xcopy solution possible - I found the way to copy files and folders but I would like to grab just the files – Michal Nov 26 '11 at 01:43
  • I have a query on this, could you pls help me with this? if there target directory already has some files then in that case i don't want to override/replace existing files and only copy the files which are newly added in source directly. – user1699227 Aug 29 '17 at 11:29
  • Unfortunately I am no longer writing DOS scripts but I suppose replacing `copy %%f x:\destination\ ` with `xcopy %%f x:\destination\ /Y` should do the work. Please check `xcopy` documentation. – kokeksibir Sep 05 '17 at 06:44
4

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.

0

The Answer to this problem which I think is "How to gather all your files out of all the little subdirectories into one single directory" is to download a piece of software called XXCOPY. This is freely available via XXCOPY.COM and there's a free non-commercial version fortunately. One of the Frequently Asked Questions on the help facility on XXCOPY.COM is effectively "How do I gather all my files into one directory" and it tells you which switch to use. XXCOPY is though a surefire way of doing this and it comes in a .zip archive so unzipping it can be not that straightforward but it's not particularly difficult either. There is an unzipping program called ZipGenius available through the ZipGenius.it website so maybe before you download XXCOPY then download ZipGenius then it's a smallpart smalltime double wammy(!)

0

Might not be the exact answer but if anyone would like to do this without coding.

You can search the name of the item inside a specific folder, and then you can copy the results and later paste it into your desired folder. It will rename the same file to be the folder I believe as the prefix and then the repeated name.