Reputation:
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...
Upvotes: 1
Views: 1540
Reputation:
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.
Upvotes: 1
Reputation: 79983
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
Upvotes: 1