Bulk rename files
So, this was the simple problem I was facing:
I have a set of mp3 files, all starting containing a particular pattern, and wanted to rename it all for whatever devious reason I had. I wanted these files to not contain the pattern anymore. For example, 000 – blah.txt and 122 – bleh.txt should be renamed to “blah.txt” and “bleh.txt”.
The options to achieve this were aplenty:
1) Wonder how convenient it would’ve been if Windows had ’sed’. However, I didn’t have, nor did I have the inclination to install Cygwin on my Windows laptop, and run sed through that. So, onto different pastures.
2) Download some free software from the web, there are many that do bulk rename.
3) Write a script of your own.
The existence of this blog entry makes the choice I made quite obvious. The reasons for my choice range from an acute affinity towards Ruby, to a decent amount of free time … :P
Here’s a script that I wrote, if you’re good at regular expressions, this might be a decent help. A list of the same can be found here.
What does one achieve?
Basically, you can rename a set of files under a particular directory, ending with a certain extension and containing a particular pattern. The resulting files will not contain the pattern anymore, but the extension would be preserved.
What to do?
1) Install Ruby
Extremely simple to install. Can be found here
2) Go to the directory where these files exist, and copy paste the above script as a RenameFiles.rb ruby file.
3) Run the command through the command line, for example: ruby RenameFiles.rb .txt [\d]+[\s]*[-]+[\s]*
Disclaimers and Warnings:
This script is obviously quite rudimentary, and has not been extensively tested either. Use at your own risk, I’m not responsible for script writing laziness on your part ! … :D
##
#
# RenameFiles.rb
#
# Function to rename files under a directory
#
# @param ext -> rename files ending with this extension
# @param pattern -> rename files containing this pattern (to be removed)
#
# @result -> Renamed files: Extension preserved, Pattern removed
#
# Note: This file has to be under the directory containing the files to be renamed
#
# A sample regular expression might be: [\d]+[\s]*[-]+[\s]* which looks for:
# [any number atleast once]->[any no. of spaces]->[hyphen]->[any no. of spaces]
#
# @author: Manoj
#
##
def RenameFiles( ext, pattern )
#
# Create regular expressions for input strings
#
pattern = Regexp.new( pattern )
ext = Regexp.new( ext + "$" )
#
# Reporting variables
#
@extensionFlag = false
@patternFlag = false
@count = 0
#
# Go through all files in the "current" directory
#
Dir.foreach(Dir.pwd) {
|x|
#
# Check if the current file carries the reqd. extension
#
if ( x =~ ext ) then
@extensionFlag = true
rptString = x.clone
rptString = rptString.gsub!(pattern, '')
#
# Was any substitution/rename made?
#
if ( rptString != nil )
@count = @count + 1
File.rename(x, rptString )
@patternFlag = true
end
end
}
#
# Report statistics ...
#
if @extensionFlag == false
puts "\nNo files with required extension"
elsif @patternFlag == false
puts "\nNo files with required pattern"
else
puts "\nRenamed " + @count.to_s + " files"
end
end
#
# Generate usage error, if insufficient params
#
if ( ARGV[0] == nil || ARGV[1] == nil )
puts "\nUsage: RenameFiles.rb \n"
else
RenameFiles( ARGV[0].to_s, ARGV[1].to_s )
end

February 9th, 2008 03:09
$args = $#ARGV + 1;
if($args < 2)
{
print “USAGE: “,$0,” “,”\n”;
exit();
}
use Cwd;
my $dir = getcwd();
opendir MYDIR, $dir;
@contents = readdir MYDIR;
foreach $file (@contents)
{
$name = $file;
if($name =~ m/(.*)($ARGV[0])(.*)\.($ARGV[1])$/)
{
print “Found match!\n”;
if(defined($1)) {$name=$1;}
if(defined($2)) {$name .= $3;}
$name .= “.”.($ARGV[1]);
rename($file,$name);
print “Renamed “,$file,” to “,$name,”\n”;
}
}
closedir MYDIR;
February 9th, 2008 03:10
Anything with files…perl rules .. :)
February 9th, 2008 03:32
hmm..my script doesn’t take regex args..aaah too lazy to work it out..
February 11th, 2008 23:53
And here I was thinking I dont have work….:)