Renaming files with wildcards

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bobt_1234
Posts: 2
Joined: 29 Aug 2011 14:29

Renaming files with wildcards

#1 Post by bobt_1234 » 29 Aug 2011 15:07

How do you rename a bunch of files using wildcards?

I'm trying to do something that seems straightforward and intuitive, but for some reason it's not working. I have a directory of files that are named (001.bmp.jpg, 002.bmp.jpg, etc.) and I'm trying to rename them to get rid of the .bmp part using the following:

rename *.bmp.jpg *.jpg

The above command has no effect.

However, I can rename files like this

rename *.bmp *.txt

and that works.

Am I missing something?

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Renaming files with wildcards

#2 Post by Ocalabob » 29 Aug 2011 19:10

Greetings bobt_1234!

That period or 'full stop' after 001 and 002 is reeking havoc with your renaming process.

I did limited testing on the script below based on your examples also no error checking for the
existence of a file named $bobt_1234.bat

Code: Select all

@echo off
setlocal
echo.@echo off>$bobt_1234$.bat
for /f "delims=" %%a in ('dir *.jpg /b') do call :go "%%a"
goto :EOF
:go
set "var=%~n1"
set var=%var:~0,3%
echo ren %~1 %var%.jpg>>$bobt_1234$.bat
endlocal


Examine $bobt_1234$.bat to ensure you are getting the desired
results.

Best wishes!

bobt_1234
Posts: 2
Joined: 29 Aug 2011 14:29

Re: Renaming files with wildcards

#3 Post by bobt_1234 » 30 Aug 2011 10:42

Wow, thanks. I didn't know you could do all that in a bat file.

I have a couple questions.

1. Why is the parameter referred to as %~n1 at the beginning but as %~1 in the rename.

2. I'm assuming the line "var=%var:~0,3% a substring command?

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Renaming files with wildcards

#4 Post by Ocalabob » 30 Aug 2011 17:37

Greetings bobt_1234

1. Why is the parameter referred to as %~n1 at the beginning but as %~1 in the rename.

2. I'm assuming the line "var=%var:~0,3% a substring command?



1. A mistake I made in the original file which I copied/pasted in my reply. %~1 should work in both places. Good eye!
2. Grabs the 001,002 from your file name examples.

Did the script work properly for you?

Best wishes!

Post Reply