Friday, July 11, 2008

Rename files using Perl expression

A wonderful solution posted. It help me for renaming files in Windows.

You see, I do some photos downloading from camera and usually the software will takes care and help rename the files automatically.

However there are times that I am stuck and need to rename these files in a batch. I do not have to do it individually (100+ or 1000+ files if not)

Glad that we have a solution here by Perl.

http://www.evolt.org/article/Renaming_Files_with_Perl/17/351/

Taken from URL above. Cheers.

#!c:\perl\bin\perl
#
# Usage: rename perlexpr [files]
use File::Glob qw(:glob);


($regexp = shift @ARGV) || die "Usage: rename perlexpr [filenames]\n";

if (!@ARGV) {
@ARGV = ;
chomp(@ARGV);
}


foreach $_ (bsd_glob(@ARGV, GLOB_NOCASE)) {
$old_name = $_;
eval $regexp;
die $@ if $@;
rename($old_name, $_) unless $old_name eq $_;
}

exit(0);

No comments: