Copy and rename file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jvuz
Posts: 38
Joined: 25 Feb 2011 03:37

Copy and rename file

#1 Post by jvuz » 29 Nov 2011 06:34

Hi,

this is my situation: I'm aving a file (test.pdf) and I want to copy it to another folder and rename it to 120001.pdf. But the goal is also to do this 200 times, so the second time it should become 120002.pdf, the next one 120003.pdf. Is this possible?

jvuz

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Copy and rename file

#2 Post by Ed Dyreen » 29 Nov 2011 06:59

'

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$filename=test"
set  "$fileext=pdf"
set     "$tdir=C:\this works"
set   "$cstart=120001"
set     "$cend=120201"
for %%? in (

   !$cstart!, 1, !$cend!

) do    copy /v /y "!$filename!.!$fileext!" "!$tdir!\!$filename!%%~?.!$fileext!"

endlocal &exit /b 0
untested !

jvuz
Posts: 38
Joined: 25 Feb 2011 03:37

Re: Copy and rename file

#3 Post by jvuz » 29 Nov 2011 07:12

Thanks a lot Ed. The result is I'm geeting now 3 files:
test1.pdf
test120001.pdf
test120201.pdf

the goal is to get
120001.pdf
120002.pdf
...
120201.pdf

But already thaks a lot.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Copy and rename file

#4 Post by Ed Dyreen » 29 Nov 2011 07:18

'
oops ! :mrgreen:

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$filename=test"
set  "$fileext=pdf"
set     "$tdir=C:\this works"
set   "$cstart=120001"
set     "$cend=120201"
for /l %%? in (

   !$cstart!, 1, !$cend!

) do    copy /v /y "!$filename!.!$fileext!" "!$tdir!\!$filename!%%~?.!$fileext!"

endlocal &exit /b 0

jvuz
Posts: 38
Joined: 25 Feb 2011 03:37

Re: Copy and rename file

#5 Post by jvuz » 29 Nov 2011 07:34

Great! The only thing is it's still showing as test120001.pdf, and so on.


I arranged it by changing the bottom command to this:

for /l %%? in (

!$cstart!, 1, !$cend!

) do copy /v /y "!$filename!.!$fileext!" "!$tdir!\%%~?.!$fileext!"

endlocal &exit /b 0

So the end file is this

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$filename=test"
set  "$fileext=pdf"
set     "$tdir=C:\this works"
set   "$cstart=120001"
set     "$cend=120201"
for /l %%? in (

   !$cstart!, 1, !$cend!

) do    copy /v /y "!$filename!.!$fileext!" "!$tdir!\%%~?.!$fileext!"

endlocal &exit /b 0

Post Reply