[Solved] I want xcopy to both copy and rename multiple files

Score
0
Vote Up
January 24, 2012 at 06:49:33 Pacific
Specs: Windows XP

I can easily use xcopy to copy files to another location

xcopy "\\Source\*.*" "\\Destination\" /y/s

but I would then like the copied files to be renamed to append "Copy of " or similar to the start of the file.

I have managed code which will replace the first 8 characters of the filename

xcopy "Source\*.*" "destination\Copy of *.*" /y /s

but haven't a way of appending this.

Can anyone help?


Jump to Best Answer ↓   Report •


#1
Vote Down
Score
0
Vote Up
January 25, 2012 at 09:11:56 Pacific

for /r source %a in (*) do copy %a "destination\Copy of %a" /y

will work from the command line, add % and \ as neccessary for batch commands

that sould do the trick for you


Reply ↓  Report •

#2
Vote Down
Score
0
Vote Up
January 26, 2012 at 02:09:52 Pacific

I have not managed to get this to work.

I created a folder called Source with a few files in and another called Destination.

I then added the following code to a batch file -

for /r H:\Source %a in (*) do xcopy %a "H:\Destination\Copy of %a" /y

But when I run the batch file all that happens is that the cmd box flashes open and closed and nothing appears in the destination folder.

The system is XP but I'm not sure why that would make any difference.


Reply ↓  Report •

#3
Vote Down
Score
1
Vote Up
January 29, 2012 at 14:32:18 Pacific

The command provided is for direct input. If you're going to put it into a script, you need to change each percent to a double percent.
for %%a in (H:\Source\*) do xcopy "%%a" "H:\Destination\Copy of %%a" /y

How To Ask Questions The Smart Way


Reply ↓  Report •

#4
Vote Down
Score
0
Vote Up
January 30, 2012 at 03:12:13 Pacific

This looks like it is at least trying to do what I want. Unfortunately, it brings up the File or Directory ? option that seems to affect others.

Irrespective of which option I then choose for each file nothing is yet copied.


Reply ↓  Report •

Related Posts

#5
Vote Down
Score
0
Vote Up
January 30, 2012 at 07:58:13 Pacific

Best Answer

Easiest solution is just to use copy instead of xcopy:
for %%a in (H:\Source\*) do copy /y "%%a" "H:\Destination\Copy of %%~NXa"
(Note: H:\Destination needs to exist before this command runs.)

How To Ask Questions The Smart Way


Reply ↓  Report •

#6
Vote Down
Score
0
Vote Up
January 30, 2012 at 08:05:38 Pacific

Okay, that worked a treat .. thank you :)

Of course the reason I had chosen to use xcopy as opposed to copy is that I had been using /y/s as the triggers because I wanted sub-directories too.

I know, I'm just being greedy !


Reply ↓  Report •

Reply to Message Icon Start New Discussion
« save input to txt file in... Monitor Directory for new... »