#1 18 Jan 2017 22:40

colombo
New Member
Registered: 18 Jan 2017
Posts: 1

Batch/Multi rename only files with a specific range

Hello, i hope someone can help me with this.

i have a bunch of consecutive  files
sometimes 10 files sometimes 22 files - it doesn't matter how much
e.g
001.xxx
002.xxx
003.xxx
004.xxx
...
i want to write a Bach file that will duplicate all files except the first and last and will rename them Consecutively to the last file
so for the e.g above it will look like this:

001.xxx
002.xxx
003.xxx
004.xxx
005.xxx
006.xxx

file 003.xxx was duplicated+renamed to 005.xxx
and file 002.xxx was duplicated+renamed to 006.xxx
first file 001.xxx and last files  in this case it was 004.xxx didn't play in this command
i did wrote this command:

copy 003.xxx 005.xxx
copy 002.xxx 006.xxx

but this command is only working if i am starting with 4 files
the problem is the amount of files i am starting with is different each time
i need your help to write something that will apply only on the 'between' files no matter how much of them

Thank you

Offline

#2 19 Jan 2017 03:49

Aacini
Member
Registered: 05 Dec 2012
Posts: 142

Re: Batch/Multi rename only files with a specific range

Your question have multiple flaws. What happen if there are files with names different than the ones in the example? What extension should be used in the new files? The assumed .xxx one or any other?. Etc...

However, the code below get the output as you requested it. This code use as input a text file instead of file names, but it is very simpe to change such a point...

@echo off
setlocal EnableDelayedExpansion

rem Create an array with existent files
set i=0
for /F %%a in (input.txt) do (
   set /A i+=1
   set "file[!i!]=%%a"
)

rem Do the requested rename
set /A n1=i-1, n2=1000+2*i-2
for /L %%i in (2,1,%n1%) do (
   ECHO copy "!file[%%i]!" "!n2:~1!.xxx"
   set /A n2-=1
)

input.txt:

001.xxx
002.xxx
003.xxx
004.xxx

output:

copy "002.xxx" "006.xxx"
copy "003.xxx" "005.xxx"

Last edited by Aacini (19 Jan 2017 03:52)

Offline

Board footer

Powered by FluxBB