- Microsoft Windows 7 Professional
- Ms-DOS Command Prompt (6.1.7601)
What?
A quick article on how to rename multiple files using the command prompt and a bit of string manipulation. This example will rename files which contain the string " (Copy)" and replace it with nothing (so removes it). The challenge here is the space character and delimiting by a string.
The Gist
-- What I have
Image00001 (Copy).jpg
Image00002 (Copy).jpg
-- What I want
Image00001.jpg
Image00002.jpg
How?
Before I continue, the undo may work in MS Windows (Control key + Z) but don't count on it. I'm going to use a short batch process but to save time on the different ways of doing this, the example below uses a command prompt to a) create a batch file b) use it to rename the files c) delete the batch file.
Open a command prompt: Method #1
- Open windows explorer to the folder you want to apply this to
- Hold down the shift key and right-click on the folder
- Select "open command window here"
Open a command prompt: Method #2
- Start > Run > CMD (or COMMAND) > Enter/OK
- Browse to folder you want to apply to this by using the "CD" command (type "CD /?" for help)
As I said, I'm going to use a short batch process (*.bat). The reason the instructions below use the command prompt are because the default setting for computers running MS Windows are set up to hide filename extensions. So you can do this quickly on a friend's PC and it's a bit geeky.
- Type the following
and answer Yes to if you want to create it.NOTEPAD rename.bat - Copy & Paste the following (the string to replace here is " (Copy)" without the quotes) - this is a READ-ONLY process if you leave the last line commented/remarked
@ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION FOR /f "tokens=*" %%a IN ('dir /b *" (Copy).jpg"') DO ( SET "oldName=%%a" SET "newName=!oldName: (Copy)=!" ECHO Rename !oldName! to !newName! REM RENAME "!oldName!" "!newName!" ) - Save the file
- Return to the command prompt and type
rename.bat - This will display all the files to be renamed
- Return to the notepad (rename.bat) and take out the remark "REM" prefix so your code becomes:
This code will rename the files. I did not find a way to undo so just run the first code to double-check what you're about to do.@ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION FOR /f "tokens=*" %%a IN ('dir /b *" (Copy).jpg"') DO ( SET "oldName=%%a" SET "newName=!oldName: (Copy)=!" ECHO Renaming !oldName! to !newName! RENAME "!oldName!" "!newName!" ) - Save and Close the notepad file
- Return to the command prompt and type
(This does the actual renaming of the files).rename.bat - Type the following
(removes it from the users computer)DELETE rename.bat - Exit the command prompt and we are Done
Other Links:
I found that using a third party was the best solution. My preference is KrojamSoft BatchRename. It's very user-friendly and lets you add suffixes/prefixes, change the characters case, rename and edit multiple files at once! Perfect for cataloging your photos and music. Thanks KrojamSoft!
Replace the command 'dir /b *" (Copy).jpg"'
as
'dir /b *"the last part of the files you want to rename"'
Also, replace the command
SET "newName=!oldName: (Copy)=!"
as
SET "newName=!oldName:_____=!"
fill in the blanks by characters that you want to rename without any space
and you are good to go 8)
Well there is always a simpler way. I suggest using BatchRenameFiles Tool to solve the problem. Thanks
Can you please help me? When I try to type rename.bat I get this error message: file not found.
what did I do wrong ?
I found this article using a google search. It was one of many in my search results.
After perusing the many examples from the other articles, this one was - bar none - the best solution.
Thanks a bunch for posting this. I needed to rename the files in a directory based on a condition from the first set of characters of each file. With a few modifications it worked like a charm!
Thanks you!!!
Scott :-)
Hello, i found this software which is really great for batch file rename in windows and it works really well. Its easy to use also. The software's Name is KrojamSoft
Batch Rename.
I suggest everyone to try KrojamSoft BatchRenameFiles program. It’s really work for me. :lol: