0

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?

3 Answers 3

1

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
1
  • 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, 2011 at 19:26
1

@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
4
  • 1
    Good answer but while it wasn't overly clear in the original question the OP is using Windows. Mar 10, 2011 at 21:23
  • I actually have access to both OS X and Windows for this! :D
    – bobber205
    Mar 10, 2011 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, 2011 at 18:38
  • @bobber205 try this find / -name "*.xml" | xargs cp {} /path/to/copy Mar 12, 2011 at 20:33
0

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)

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .