# Copying Thousands of Files from a search result to one directory

I have a lot of identically named "data.xml" files on my system in different directories.

A basic Windows search easily finds all of these. I would love to be able to do a search on these, find them, and copy them to a directory so there are named data_1.xml etc. etc.

Just the fact that they're all in one directory is what I'm aiming for.

I've tried using Teracopy to do the heavy lifting of copying but it's not renaming the files correctly. Is there any tool out there for something like this?

-

Just for completeness, here's a Windows-based solution to run in the CMD shell:

@echo off

set SRC=c:\source
set DST=c:\dest
set FN=0

for /F "usebackq delims=" %%i in (dir /s /b %SRC%\*.xml) do call :docopy "%%i"
goto end

:docopy
set /A FN=%FN% + 1
echo copy %1 "%DST%\%~n1_%FN%%~x1"
:end

-
Due to some error on my part or the solution's response, I booted up Windows and gave this a go. While I had to add an actual copy line instead of just the echo line, it worked great! :D – bobber205 Mar 11 '11 at 19:26

@bobber205

find / -name "*.xml" -exec cp  {} ./ \;


Should do the job.

Store this script say rename.sh and then ./rename.sh

this would rename them

#!/bin/sh
i=1
for j in ls *.xml
do
orig=$j echo$orig
mv $orig orig$i.xml
i=expr \$i + 1
done


EDIT

another way to copy is

 find / -name "*.xml" | xargs cp {} /path/to/copy

-
Good answer but while it wasn't overly clear in the original question the OP is using Windows. – John Gardeniers Mar 10 '11 at 21:23
I actually have access to both OS X and Windows for this! :D – bobber205 Mar 10 '11 at 21:43
Thanks so much for your solution! A++++++++ – bobber205 Mar 10 '11 at 21:43
Just now got some time to try this out and the find command replaces the info.xml file with the next one it copies so I only end up with one. :( – bobber205 Mar 11 '11 at 18:38
@bobber205 try this find / -name "*.xml" | xargs cp {} /path/to/copy – Registered User Mar 12 '11 at 20:33

The previously suggested solution failed im my case when the target directory contained files and directories with whitespaces and non-latin characters in their names. It turns out Windows (Vista & and above) comes with a command line tool for robust copy, called Robocopy. The usage is very simple:

robocopy c:\soruce\from f:\target\to


Worked very well. (My thanks go to Chris Hoffman from makeuseof.com for his post that gave me this idea)

-
or just find / -name "*.xml" -print0 | xargs -0 cp -t /path/to/copy – rudimeier Dec 29 '13 at 12:12