2

In Windows XP, I'm trying to figure out how to batch rename and remove the last characters of filenames.

Example of removing last 4 characters before file extenstion: file.doc.pdf --> file.pdf

I could do:

ren *.pdf *.
ren *.doc *.pdf

Though this wouldn't work well if there are already other PDFs in the folder.

Not sure if the FOR command is needed here.

2
  • For removing: What about using two dots? ren *.doc. *. This way other files with .doc extension don't get touched.
    – nixda
    Jun 15, 2013 at 22:15
  • @nixda: *.doc. won't work (file not found).
    – Karan
    Jun 16, 2013 at 0:22

4 Answers 4

2

If your file names do not contain any periods other than at the end (.doc.pdf), the following will work:

for /f "delims=." %a in ('dir /b *.doc.pdf') do ren "%~a.doc.pdf" "%~a.pdf"
5
  • If you want you can insert an echo before ren to preview all the renaming operations.
    – Karan
    Jun 16, 2013 at 0:36
  • Karan, your command works, but only if I paste it into the command window, not from the batch file. Any ideas? Jun 16, 2013 at 1:59
  • Of course, in a batch file you must double every % sign. So each % will become %%. Try it and see!
    – Karan
    Jun 16, 2013 at 2:13
  • BTW, if this and my previous answer helped you don't forget to accept them using the green check mark to the left (click it so it becomes filled). Thanks!
    – Karan
    Jun 16, 2013 at 2:47
  • @Karan - There is actually a surprisingly simple solution using only a single REN command :) See my answer
    – dbenham
    Mar 29, 2014 at 2:52
2

ren * *. -> will give you 'file.doc' repeat above command -> will give you 'file' ren * *.pdf -> will give 'file.pdf'

0
ren *.doc.pdf ????????????????????.pdf

Just make sure the target mask has at least as many ? as the longest base file name.

For an explanation as to why this works, see How does the Windows RENAME command interpret wildcards?

-1

file.doc.pdf --> file.doc:

for %I in (*.doc.pdf) do rename "%~nI.pdf" "%~nI"

file.doc --> file.pdf

for %I in (*.doc) do rename "%~nI.doc" "%~nI.pdf"
1
  • Sorry, but -1. You need to use quotes or both commands will fail if the filenames have spaces. Also, instead of renaming .doc.pdf to .pdf, your first command will actually rename to .doc
    – Karan
    Jun 16, 2013 at 0:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .