Batch Process to rename multiple files using Windows DOS

Applies to:
  • 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
  1.  -- What I have 
  2.  Image00001 (Copy).jpg 
  3.  Image00002 (Copy).jpg 
  4.   
  5.  -- What I want 
  6.  Image00001.jpg 
  7.  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
  1. Open windows explorer to the folder you want to apply this to
  2. Hold down the shift key and right-click on the folder
  3. Select "open command window here"

Open a command prompt: Method #2
  1. Start > Run > CMD (or COMMAND) > Enter/OK
  2. 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.

  1. Type the following
    copyraw
    NOTEPAD rename.bat
    1.  NOTEPAD rename.bat 
    and answer Yes to if you want to create it.
  2. 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
    copyraw
    @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!"
    )
    1.  @ECHO OFF 
    2.  SETLOCAL ENABLEDELAYEDEXPANSION 
    3.  FOR /f "tokens=*" %%a IN ('dir /b *(Copy).jpg"') DO ( 
    4.  SET "oldName=%%a" 
    5.  SET "newName=!oldName: (Copy)=!" 
    6.  ECHO Rename !oldName! to !newName! 
    7.  REM RENAME "!oldName!" "!newName!" 
    8.  ) 
  3. Save the file
  4. Return to the command prompt and type
    copyraw
    rename.bat
    1.  rename.bat 
  5. This will display all the files to be renamed
  6. Return to the notepad (rename.bat) and take out the remark "REM" prefix so your code becomes:
    copyraw
    @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!"
    )
    1.  @ECHO OFF 
    2.  SETLOCAL ENABLEDELAYEDEXPANSION 
    3.  FOR /f "tokens=*" %%a IN ('dir /b *(Copy).jpg"') DO ( 
    4.  SET "oldName=%%a" 
    5.  SET "newName=!oldName: (Copy)=!" 
    6.  ECHO Renaming !oldName! to !newName! 
    7.  RENAME "!oldName!" "!newName!" 
    8.  ) 
    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.
  7. Save and Close the notepad file
  8. Return to the command prompt and type
    copyraw
    rename.bat
    1.  rename.bat 
    (This does the actual renaming of the files).
  9. Type the following
    copyraw
    DELETE rename.bat
    1.  DELETE rename.bat 
    (removes it from the users computer)
  10. Exit the command prompt and we are Done

Other Links:
Category: MS-DOS :: Article: 574

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.