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.

Infinite loop
  • 39
  • 1
  • 1
  • 5

4 Answers4

2

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

Zimba
  • 1,051
  • 11
  • 15
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"
Karan
  • 55,947
  • 20
  • 119
  • 191
  • If you want you can insert an `echo` before `ren` to preview all the renaming operations. – Karan Jun 16 '13 at 00: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 01: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 02:13
  • BTW, if this and my previous answer helped you don't forget to [accept them](http://superuser.com/help/someone-answers) using the green check mark to the left (click it so it becomes filled). Thanks! – Karan Jun 16 '13 at 02:47
  • @Karan - There is actually a surprisingly simple solution using only a single REN command :) See [my answer](http://superuser.com/a/734969/109090) – dbenham Mar 29 '14 at 02:52
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?

dbenham
  • 11,194
  • 6
  • 30
  • 47
-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"
STTR
  • 6,767
  • 2
  • 18
  • 20
  • 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 00:25