Command Line (or batch file) rename all files in a folder (Windows)
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;
REDH开发者_运维百科I20806-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
short answer
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
examples
Windows Example
Given
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
Doing It
PS C:\rename-me> ls | foreach-object -process {ren $_ (%{$_ -replace "-straight",""})}
Result
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
Unix/Linux Example
Given
$ 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
Doing It
$ ls | awk '{print("mv "$1" "$1)}' | cut -f -3,5- -d '-' | sh
Result
$ 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
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)
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
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.
精彩评论