Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Use this one...' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use this one...
Authored by: bimtob on Jul 12, '05 07:40:46PM
One more time. It ate my backslashes.

#!/usr/bin/perl
# remv: Regular Expression mv
# Usage: remv [-t | -r] filter "regular expression"
# -t is to test regex
# -r is to perform the mv operation
# filter is an argument to ls (ex. *.pl)
# regular expression is put in quotes. $s are backslash \escaped
#
# Example: remv -t \*.jpg "s/oldname(\d)(.jpg)/newname\$1\$2/"
#

$mode = shift;
$filter = shift;
$regex = shift;
$str = "\$file =~ $regex";
chomp($filter);
chomp($regex);

@filelist = `ls $filter`;

if ($mode eq "-r") {
	foreach $file (@filelist) {
		chomp($file);
		$oldname = $file;
		eval $str;
		print "renaming: $oldname to $file\n";
		`mv $oldname $file`;
	}
}
elsif ($mode eq "-t") {
	foreach $file (@filelist) {
		chomp($file);
		$oldname = $file;
		eval $str;
		print "$oldname -> $file\n";
	}
}
else { print "-t\ttest regex\n-r\trename files\n";}
[\code]


[ Reply to This | # ]