Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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?

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 *.aviThe normal warnings apply; test first ect.

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.

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!

"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...

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?"

Klint,
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.

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.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |