Computing.Net > Forums > Programming > batch rename file extension?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch rename file extension?

Reply to Message Icon

Name: anon0
Date: February 2, 2009 at 17:41:21 Pacific
OS: Windows Vista
CPU/Ram: C2D, 2GB
Product: N/a / NA
Subcategory: Batch
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: February 3, 2009 at 00:06:30 Pacific
Reply:

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.


0

Response Number 2
Name: klint
Date: February 3, 2009 at 03:35:15 Pacific
Reply:

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.


0

Response Number 3
Name: anon0
Date: February 3, 2009 at 14:27:12 Pacific
Reply:

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!


0

Response Number 4
Name: Judago
Date: February 4, 2009 at 00:34:28 Pacific
Reply:

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


0

Response Number 5
Name: klint
Date: February 4, 2009 at 02:02:24 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Judago
Date: February 4, 2009 at 04:13:00 Pacific
Reply:

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.


0

Response Number 7
Name: Mechanix2Go
Date: February 4, 2009 at 10:08:41 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch rename file extension?

batch rename files with a part of.. www.computing.net/answers/programming/batch-rename-files-with-a-part-of/16747.html

batch rename files consecutive n° www.computing.net/answers/programming/batch-rename-files-consecutive-n/15480.html

bat: Rename files w/double ext? www.computing.net/answers/programming/bat-rename-files-wdouble-ext/16440.html