batch rename file extension?

February 2, 2009 at 17:41:21
Specs: Windows Vista, C2D, 2GB
I have files of the form *.mpg.avi that I need to rename as *.avi (get rid of the .mpg part), using a batch file in the Windows command line environment.

rename *.mpg.avi *.avi doesn't do anything. I think the answer has to do with "for %f in (*.mpg.avi) do ren "%f" <SOMETHING>" but I'm not sure what the something is. Any ideas?

See More: batch rename file extension?

Report •

February 3, 2009 at 00:06:30
Provided you don't have any files with the extension of .mp in the same directory this may work:

ren *.mpg.avi *p
ren *.mp *.avi

The normal warnings apply; test first ect.

Report •

February 3, 2009 at 03:35:15
Just in case Judago's method doesn't work, you can try the following, also untested.

for %%f in (*.mpg.avi) do (
   for %%g in ("%~nf") do (
      ren "%%f" "%%~ng%%~xf"

How it works: the first for-loop goes through each file in the directory. %f is filename.mpg.avi, %~nf is filename.mpg and %~xf is .avi

The second for-loop deals with just one file each time. It takes filename.mpg and gets the filename part (%~ng).

The double %% is required whenever you use a for loop in a batch file. Use just one % if you just type the above straight on the command line.

Report •

February 3, 2009 at 14:27:12
judago's method was interesting! Does "ren *.mpg.avi *p" mean "keep only the part of the filename before the last p"?

klint's method worked too and is the intended method (the second line was missing a %, otherwise it's good). Thanks to you both!

Report •

Related Solutions

February 4, 2009 at 00:34:28
"Does "ren *.mpg.avi *p" mean "keep only the part of the filename before the last p"?"

Pretty much except I would say "keep only the part of the filename up to and including the last p"

If you try it in reverse it will only overwrite one character at a time:

ren abcdef c*

Will result in cbcdef...

Report •

February 4, 2009 at 02:02:24
Judago, this "ren *.mpg.avi *p" magic is fascinating. Is it documented anywhere? Or is it an "unintended feature" or "bug that just happens to be useful?"

Report •

February 4, 2009 at 04:13:00

Belive it or not I actually picked it up from you! ( -> link).

I did some testing and found the trick to it, it deletes everything after the last occurrence of the specified character. In your post you used the character "." which windows doesn't seem to allow file names to end in, so it to gets trimmed off, leaving the rest of the file name.

It actually seems to have an (inverted) parallel to the "Enter char to delete up to:" function from hitting f4 on the command line. (by the way does anyone know how to harness the fx functions within a script without user input?)

So to answer you questions I am unaware if it was intended or is documented.

Report •

February 4, 2009 at 10:08:41
Prety neat stuff. I don't know if it's documented or intended.

But it's good to keep in mind the difference between a feature and a bug. A feature has seniority.

If at first you don't succeed, you're about average.


Report •

Ask Question