1

At first: it's not a repeated question (I hope). I've searched a lot and found How to find files with no file extension in directory tree with a batch file? and Find and rename files with no extension?, among others, but I have a "small" additional problem.

I'm trying to rename multiple (thousands) of jpg files on Windows 10. The name format is "Name (ABC).jpg" or "Multiple Names (ABC).jpg". I need "Some Name.jpg".

What have you tried? So many things, none of them a real solution, until this:

for /f "tokens=1-3 delims=)(" %%a in ('dir /b *.jpg') do @rename "%%a(%%b)%%c" "%%a"
for /R %%x in (*.) do ren "%%x" *.png

It works fine for most of the files, except when there's a dot in the name. If the name is something like "A Name.Supercool (ABC).jpg", it will be only "A Name.Supercool", with no extension.

I know the problem is on (*.), just don't know how to solve...

Daniel Lemes
  • 279
  • 4
  • 21
  • 2
    "*What have you tried? So many things, none of them a real solution*" - and yet in powershell a real solution is just a `gci *.jpg | ren -newname { ($_.basename -replace '\s+\(.*\)') + ".png"}` away. – TessellatingHeckler Jun 09 '17 at 02:59
  • Much better solution, thank you. It could be the accepted answer. – Daniel Lemes Jun 09 '17 at 03:16

2 Answers2

1

Your problem is that renaming your files to "%%a" produces "A Name.Supercool" - and that this has "no extension".

Ah - but it does. The filename is "A Name" and the extension is ".Supercool"

Were you to rename your file to `"%%a%%c" then the name would become "A Name.Supercool.jpg".

Now why, after attempting to rename your file to extensionless, you are then renaming them to *.png I've no idea, and you don't explain, unfortunately. Why wouldn't you simply tack on the .png to the %%a - if that truly was what you wanted to do?

(BTW - the for /f ... dir... approach builds a memory-list of the files, then renames them, so they won't be re-processed as may occur with for %%x in (*.jpg)...)


Given the terminal-space-in-name problem,

for /f "tokens=1-3 delims=)(" %%a in ('dir /b *.jpg') do rename "%%a(%%b)%%c" "%%a%%c"
SETLOCAL enabledelayedexpansion
for /f "delims=" %%a in ('dir /b "* .jpg"') do (
 SET "name=%%a"
 REN "%%a" "!name:~0,-5!.jpg"
)
endlocal
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 2
    `"Multiple Names (ABC).jpg"` split on the parens has a space at the end of Names, and if you tack on `.png` then you get `"Multiple Names .png"` with a space in it. I speculate that's why they aren't doing it. – TessellatingHeckler Jun 09 '17 at 02:56
  • I tried "%%a(%%b)%%c" "%%a%%c", but it puts a space before the extension, like "A Name .jpg" (edit: better saying, the space stays there). Then I did it (first make it extensionless and then rename). Probably a bad idea for someone who knows what to do... – Daniel Lemes Jun 09 '17 at 03:06
  • Wouldn't it be better to pass `%%a` to a subroutine that removes trailing spaces? Then it can do `ren "%%a(%%b)%%c" "!name!.png"` just once. – Eryk Sun Jun 09 '17 at 03:37
  • Edition tested. Works with a small flaw: if the name is like "Nice Name! (USA).jpg", the space is not removed. – Daniel Lemes Jun 09 '17 at 03:41
  • Yes - you're likely to get that in batch when the name contains characters that have a special meaning to batch - `%` would no doubt suffer the same problem. – Magoo Jun 09 '17 at 03:57
1

Magoo's answer is closer to what I asked, but there is a problem with special characters in file names. TessellatingHeckler posted the best approach as a comment, so here it goes. Batch file:

powershell -command "gci *.jpg | ren -newname { ($_.basename -replace '\s+\(.*\)') + '.jpg'};gci *.png | ren -newname { ($_.basename -replace '\s+\(.*\)') + '.png'}"

With this I can handle .jpg and .png at once, flawlessly. Thank you all.

Daniel Lemes
  • 279
  • 4
  • 21