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.

  • For removing: What about using two dots? ren *.doc. *. This way other files with .doc extension don't get touched. – nixda Jun 15 '13 at 22:15
  • @nixda: *.doc. won't work (file not found). – Karan Jun 16 '13 at 0:22
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"
| improve this answer | |
  • If you want you can insert an echo before ren to preview all the renaming operations. – Karan Jun 16 '13 at 0:36
  • Karan, your command works, but only if I paste it into the command window, not from the batch file. Any ideas? – Infinite loop Jun 16 '13 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 '13 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 '13 at 2:47
  • @Karan - There is actually a surprisingly simple solution using only a single REN command :) See my answer – dbenham Mar 29 '14 at 2:52
2

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

| improve this answer | |
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?

| improve this answer | |
-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"
| improve this answer | |
  • 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 '13 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.