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
  • 2
  • Welcome to Stack Overflow! What have you tried? –  Mar 04 '13 at 18:55
  • Tried to rename like above with no success. It does work with a source filename without "." in the middle. I could find a workaround by deleting the "." in the filename –  Mar 04 '13 at 19:04
  • 1
    James L has a good solution for your problem. The RENAME command alone cannot do what you want. See [How does the Windows RENAME command interpret wildcards?](http://superuser.com/q/475874/109090) to get an idea of what you *can* do with RENAME. – dbenham Mar 04 '13 at 23:06
  • Thanks dbenham, this is like a goldmine, it's on my bookmarks already :) – ciquta Mar 05 '13 at 10:21

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
  • 2
  • 15
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.