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?

John Gardeniers
  • 27,458
  • 12
  • 55
  • 109
bobber205
  • 241
  • 1
  • 6
  • 17

3 Answers3

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
Registered User
  • 1,463
  • 5
  • 18
  • 37
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
Evan Anderson
  • 141,881
  • 20
  • 196
  • 331
  • 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
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)