- 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
copyraw
-- What I have Image00001 (Copy).jpg Image00002 (Copy).jpg -- What I want Image00001.jpg Image00002.jpg
- -- 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.
- 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/remarkedcopyraw
@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!" )
- @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
- 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:copyrawThis 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!" )
- @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).
- Type the following (removes it from the users computer)
- Exit the command prompt and we are Done
Other Links:
Category: MS-DOS :: Article: 574
Add comment