Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

renaming files in cmd using wildcards

I'm writing a BAT file for renaming some files. My files are *.jpg where * is the file name and I want them to be *-thumb.jpg.

When trying to do RENAME *.jpg *-thumb.jpg my files appear as *.jpg-thumb.jpg.

like image 819
kru96 Avatar asked Jan 08 '12 18:01

kru96


2 Answers

Actually the RENAME command alone can solve the problem, as long as there is only one . in the name. Simply use many ? instead of a single *.

REN *.jpg ????????????????????-thumb.*

Just make sure the number of ? is at least as long as the longest file name that will be renamed.

You can ensure that names with multiple . are not accidentally named by using many ? in the source mask as well. The following will rename "abc.jpg" to "abc-thumb.jpg", but will leave "abc.xyz.jpg" alone.

REN ????????????????????.jpg ????????????????????-thumb.*

You can rename a file with two dots like "abc.xyz.jpg" using the following.

REN ????????????.????????????.jpg ????????????.????????????-thumb.*

The pattern can be extended for any number of dots in the name.

See How does the Windows RENAME command interpret wildcards? for more info.

like image 69
dbenham Avatar answered Sep 19 '22 11:09

dbenham


Something like:

for %%f in (*.jpg) do rename "%%f" "%%~nf-thumb.jpg"
like image 30
Peter Svensson Avatar answered Sep 21 '22 11:09

Peter Svensson