rename base on a number in other files'name

August 21, 2010 at 02:34:28
Specs: Windows XP
Hello all, I just register this acc to ask a question (maybe it's duplicate, but I'm lazy for searching so I post it):
How do I rename a file like the title. For example:
I have some file:
pfile.xp3
patch.xp3
patch2.xp3
.......
patchx.xp3
x is a random number
I want rename pfile.xp3 to patchx+1.xp3
If x=7 so pfile.xp3 is renamed patch8.xp3.

Someone help me, please.


See More: rename base on a number in other filesname

Report •

#1
August 21, 2010 at 10:08:03
this will work if the number is only 1 digit (1-9). I haven't quite figured out how to get it to work with 2 digits.

@ECHO OFF

FOR /f "tokens=*" %%a IN ('DIR /b /o:n *.xp3') DO (
	ECHO %%a>>TempFile.txt
)

PAUSE

FOR /f %%a IN ('FINDSTR /i patch TempFile.txt') DO (
	SET Var=%%a
)

DEL TempFile.txt

SET Var=%Var:~5,1%
SET /a Var +=1

RENAME pfile.xp3 patch%Var%.xp3


Report •

#2
August 22, 2010 at 02:40:21
Thanks for your hard work
I also have my own solution but it's so long and... I don't know how to represent it:
...
if exist patch4.xp3 goto patch5 else (
rename pfile.xp3 patch4.xp3
echo patch4.xp3 > fileup.txt 
)
:patch5
if exist patch5.xp3 goto patch6 else (
echo patch5.xp3 > fileup.txt 
rename pfile.xp3 patch5.xp3
)
:patch6
if exist patch6.xp3 goto patch7 else (
echo patch6.xp3 > fileup.txt 
rename pfile.xp3 patch6.xp3
)
:patch7
if exist patch7.xp3 goto patch8 else (
echo patch7.xp3 > fileup.txt 
rename pfile.xp3 patch7.xp3
)
:patch8
if exist patch8.xp3 goto patch9 else (
echo patch8.xp3 > fileup.txt 
rename pfile.xp3 patch8.xp3
)
:patch9
if exist patch9.xp3 goto patch10 else (
echo patch9.xp3 > fileup.txt 
rename pfile.xp3 patch9.xp3
)
...

Plus, I push the file name into a "txt" file after renaming.

Report •

#3
August 22, 2010 at 03:18:23
download gawk for windows then save the script below as myrename.awk.

BEGIN{
 t=0
 for(i=1;i<=ARGC;i++){
   filename=ARGV[i]
   gsub(/patch|\.xp3/,"",ARGV[i])
   if(ARGV[i]>t){
    t=ARGV[i]
   }
 }
 print "highest number is: "t
 print "next number is: "t+1
 print "renaming pfile to: patch"t+1".xp3"
 cmd="ren pfile patch"t+1".xp3"
 print cmd
 # system(cmd) #uncomment to do actual renaming
}

on the command line:

c:\test> gawk.exe -f  myrename.awk patch*

GNU win32 packages | Gawk


Report •
Related Solutions


Ask Question