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:
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 •

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.


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


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 •

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 
if exist patch5.xp3 goto patch6 else (
echo patch5.xp3 > fileup.txt 
rename pfile.xp3 patch5.xp3
if exist patch6.xp3 goto patch7 else (
echo patch6.xp3 > fileup.txt 
rename pfile.xp3 patch6.xp3
if exist patch7.xp3 goto patch8 else (
echo patch7.xp3 > fileup.txt 
rename pfile.xp3 patch7.xp3
if exist patch8.xp3 goto patch9 else (
echo patch8.xp3 > fileup.txt 
rename pfile.xp3 patch8.xp3
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 •

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

 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