copy and rename files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
novmar
Posts: 9
Joined: 09 Oct 2013 11:10

copy and rename files

#1 Post by novmar » 18 Oct 2013 05:04

Hi,
I don't know how to do this, so please help me.
I have "xyz.jpg" file and I want to copied several times the same .jpg file and rename into x-folder, but name must read from x.txt file.
xyz.jpg must stay unchanged.

xyz.jpg -> c:\x-folder\txt(first line).jpg ... 3865.jpg
xyz.jpg -> c:\x-folder\txt(second line).jpg ... 42578.jpg
xyz.jpg -> c:\x-folder\txt(third line).jpg ... 359871.jpg
xyz.jpg -> c:\x-folder\txt(fourth line).jpg ... 2589.jpg
...

x.txt file
3865
42578
359871
2589
...
Last edited by novmar on 20 Oct 2013 10:50, edited 2 times in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: copy and rename files

#2 Post by foxidrive » 18 Oct 2013 12:28

use a for /f loop to read the file and a copy command inside the loop.

novmar
Posts: 9
Joined: 09 Oct 2013 11:10

Re: copy and rename files

#3 Post by novmar » 19 Oct 2013 07:38

tnx for help foxidrive, I made code what I need

For /f %%a in (x.txt) do copy C:\x-folder\xyz.jpg > c:\test\%%a.jpg

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: copy and rename files

#4 Post by foxidrive » 19 Oct 2013 08:09

novmar wrote:tnx for help foxidrive, I made code what I need

For /f %%a in (x.txt) do copy C:\x-folder\xyz.jpg > c:\test\%%a.jpg



That will almost work for you, if the > is removed.

For future use and to support long filenames then this is a little more robust:

Code: Select all

For /f "delims=" %%a in (' type "x.txt" ') do copy "C:\x-folder\xyz.jpg"  "c:\test\%%a.jpg"

novmar
Posts: 9
Joined: 09 Oct 2013 11:10

Re: copy and rename files

#5 Post by novmar » 20 Oct 2013 10:46

yes, without > is work
tnx

I have one question +

if I have x.txt with...
date;name-x;12456
date;name-x;1212
data;name-y;1213
data;name-y;57213
data;name-y;125513
data;name-z;11300


and *.jpg file with name
name-x.jpg
name-y.jpg
name-z.jpg

now, I know how set code if I have one .jpg

Code: Select all

For /f "tokens=3 delims=;" %%a in (x.txt) do copy C:\x-folder\name-x.jpg c:\x-folder\Renamed\%%a.jpg


but how to set a code if i have several .jpg files whitch we have to rename
IF in x.txt name-x = name-x.jpg > copy and rename 12456.jpg
IF in x.txt name-x = name-x.jpg > copy and rename 1212.jpg
IF in x.txt name-y = name-y.jpg > copy and rename 1213.jpg ...

novmar
Posts: 9
Joined: 09 Oct 2013 11:10

Re: copy and rename files

#6 Post by novmar » 20 Oct 2013 13:34

I found one solution and is work

Code: Select all

For /f "tokens=2,3 delims=;" %%a in (x.txt) do copy C:\x-folder\%%a.jpg c:\x-folder\Renamed\%%b.jpg


but this code doesn't recognize CRO letters (ščćžđ)

Post Reply