Reputation: 151
I need to rename all of the files in a folder with the following filename format;
itemID-straight-bottle.png
TO
itemID-bottle.png
How can i accomplish this with a cmd script or in the command line?
example;
REDHI20806-straight-bottle.png TO REDHI20806-bottle.png
I should have said, this is in Windows and i want to use either command line or a batch file to run this renaming on all files in a folder on a specific drive
Upvotes: 1
Views: 16932
Reputation: 8396
Personally, I would use a tool designed specifically for bulk renaming, e.g. renamer.
This command strips out the -straight
text from all files in the current directory:
$ renamer --find=-straight --dry-run *
Dry run
✔︎ REDHI20806-straight-bottle.png → REDHI20806-bottle.png
✔︎ GRNHI30030-straight-bottle.png → GRNHI30030-bottle.png
Rename complete: 2 of 2 files renamed.
Upvotes: 0
Reputation: 957
I ended up going with a modified version of wimh's answer but instead of "??????????", I used "*" which I think better handles names of different lengths.
ren *-straight-bottle.png *-bottle.png
Upvotes: 0
Reputation: 22499
On Windows, you can use PowerShell, that is installed by default on Windows 7, and can be downloaded and installed on previous versions. With PowerShell you can do the rename as:
ls | foreach-object -process {ren $_ (%{$_ -replace "-straight",""})}
On Unix/Linux, nothing specific needs to be installed, and you can do the rename as:
ls | awk '{print("mv "$1" "$1)}' | cut -f -3,5- -d '-' | sh
PS C:\rename-me> ls
Directory: C:\rename-me
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 10/9/2011 1:35 PM 0 REDHI20806-straight-bottle.png
-a--- 10/9/2011 1:35 PM 0 REDHI20807-straight-bottle.png
-a--- 10/9/2011 1:35 PM 0 REDHI20808-straight-bottle.png
-a--- 10/9/2011 1:35 PM 0 REDHI20809-straight-bottle.png
-a--- 10/9/2011 1:35 PM 0 REDHI20810-straight-bottle.png
PS C:\rename-me> ls | foreach-object -process {ren $_ (%{$_ -replace "-straight",""})}
PS C:\rename-me> ls
Directory: C:\rename-me
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 10/9/2011 1:35 PM 0 REDHI20806-bottle.png
-a--- 10/9/2011 1:35 PM 0 REDHI20807-bottle.png
-a--- 10/9/2011 1:35 PM 0 REDHI20808-bottle.png
-a--- 10/9/2011 1:35 PM 0 REDHI20809-bottle.png
-a--- 10/9/2011 1:35 PM 0 REDHI20810-bottle.png
$ ls -l
total 0
-rw-r--r-- 1 user staff 0 Oct 7 00:54 REDHI20806-straight-bottle.png
-rw-r--r-- 1 user staff 0 Oct 7 00:54 REDHI20807-straight-bottle.png
-rw-r--r-- 1 user staff 0 Oct 7 00:54 REDHI20808-straight-bottle.png
-rw-r--r-- 1 user staff 0 Oct 7 00:54 REDHI20809-straight-bottle.png
-rw-r--r-- 1 user staff 0 Oct 7 00:54 REDHI20810-straight-bottle.png
$ ls | awk '{print("mv "$1" "$1)}' | cut -f -3,5- -d '-' | sh
$ ls -l
total 0
-rw-r--r-- 1 user staff 0 Oct 7 00:54 REDHI20806-bottle.png
-rw-r--r-- 1 user staff 0 Oct 7 00:54 REDHI20807-bottle.png
-rw-r--r-- 1 user staff 0 Oct 7 00:54 REDHI20808-bottle.png
-rw-r--r-- 1 user staff 0 Oct 7 00:54 REDHI20809-bottle.png
-rw-r--r-- 1 user staff 0 Oct 7 00:54 REDHI20810-bottle.png
Upvotes: 7
Reputation: 15232
if only the last part of the filename (straight-bottle.png
) needs to change (to bottle.png
) you can simply do this:
REN ??????????-straight-bottle.png ??????????-bottle.png
(I did not test this though)
Upvotes: 2