-2

I have a windows folder having 500+ files with file names ending with the string " 203"
Example:
1X 203.bmp
2X 203.bmp
1086 203.bmp
25G 203.bmp
...

How can i bulk rename all files in this folder, removing the " 203" string from the file names? Files after renaming should be like this..

1X.bmp
2X.bmp
1086.bmp
25G.bmp

Stephan
  • 41,764
  • 65
  • 238
  • 329
MK07
  • 1
  • 1
  • 1
  • 4
  • Have you thought of using a Perl program's glob and mv (ie: rename too) ? Its almost a 1 liner. –  Feb 17 '14 at 20:11
  • Related: http://stackoverflow.com/questions/5208254/renaming-a-group-of-files-at-the-same-time-using-a-regex-to-concatenate-a-numb – herohuyongtao Feb 17 '14 at 20:14
  • Consider using a tool like [ReNamer](http://www.den4b.com/?x=products&product=renamer) if this is a one off task. EDIT: TThere are many good renaming tools: http://superuser.com/questions/16007/how-can-i-mass-rename-files-from-the-command-line-or-using-a-3rd-party-tool – Dean Taylor Feb 17 '14 at 20:21
  • For Perl, you could rename it by globing a list of files, then `$res = File::Copy::move ("$fr", "$to")` for the rename. –  Feb 17 '14 at 20:21
  • This was a one off task. ReNamer worked perfectly for me. THANK YOU Dean! – MK07 Feb 18 '14 at 16:05

3 Answers3

3

Since you are running on Windows, you can take advantage of PowerShell:

dir | rename-item –newname { $_.name –ireplace '(.+?) 203\.bmp$','$1.bmp' }

Description

dir          => list the content of the current directory
 |           => send each found element to the next command
rename-item  => rename an element in a Windows PowerShell provider namespaceject
–newname     => specify the name of the renamed file
{
    $_               => points to an object representing the actual file
   .name             => the name property of the automatic variable
   -ireplace         => perform a insensitive replace
   '(.+?) 203\.bmp$' => here comes the regex for matching the desired files
   '$1.bmp'          => the replacement string
}

You must run this command inside a power shell. Here is how to start it:

how to launch powershell on Windows 7?

Then cd into you dir from the Powershell window:

Change to the directory containing the files to be renamed from powershell window

Stephan
  • 41,764
  • 65
  • 238
  • 329
1
for /f "delims=" %i in ('dir /b /a-d "* 203.bmp"') do @set "name=%i"&call set "name=%name: 203.bmp=.bmp%"&call ren "%i" "%name%"&set "name="

directly from the prompt as a batch one-liner

(I'd suggest you try it on a (part-)copy of your directory first though)

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Your CALL trick to simulate delayed expansoin is broken - you need to double the percents. – dbenham Feb 18 '14 at 01:13
  • @dbenham: not from the prompt it isn't!. Now if it was a line in a batch file - that's another matter, but directly from the prompt - as posted! (I've edited the bold text to include `directly`) – Magoo Feb 18 '14 at 01:22
  • Ooh - my bad. But the variable may already exist prior to executing the command, which would cause it to fail. That can be fixed by putting a caret anywhere within the variable name - for example: `call set "name=%^name: 204.bmp=.bmp%"` – dbenham Feb 18 '14 at 01:28
0

For the people who are looking for software tool based solution instead of writing batch script.

Try the free tool(Windows only): Bulk Rename Utility

http://www.bulkrenameutility.co.uk/Download.php

enter image description here

SridharKritha
  • 8,481
  • 2
  • 52
  • 43