5

I have many files named like this:
YYY.XXXXXX

and i need to write a DOS batch command to rename like this:
YYYXXXXXX.ZZZ

YYY and ZZZ are fixed string, only XXXXXX is variable.

tried this with no success:
rename YYY.?????? YYY??????.ZZZ

ciquta
  • 51
  • 1
  • 1
  • 3

2 Answers2

10

You can use the for statement to do this because it gives you access to the filename and the extension separately:

for /f "tokens=1* delims=." %i in ('dir /b yyy.*') do ren "%i.%j" "%i%j.zzz"

Using tokens=1,2 delims=. causes it to split the value returned by dir /b yyy.* on the . into the %i and %j variables, where %i is the filename (or 'yyy'), and %j is the variable extension (without the leading dot .).

Use the command above if you are typing it directly from the command prompt. From a batch file, you need to double all of the % symbols like this:

for /f "tokens=1* delims=." %%i in ('dir /b yyy.*') do ren "%%i.%%j" "%%i%%j.zzz"

Make sure you run this command from the folder where all of the yyy.xxxxxx files reside.

James L.
  • 402
0

Just as a point, before trying to use ren or any other command, first try a echo to see what would be done, so instead of:

for /f "tokens=1* delims=." %i in ('dir /b yyy.*') do ren "%i.%j" "%i%j.zzz"

First do (to see what commands would be run):

for /f "tokens=1* delims=." %i in ('dir /b yyy.*') do @(echo ren "%i.%j" "%i%j.zzz")

After the output show that the commands are what you want, just remove the echo.