Skip to content

Instantly share code, notes, and snippets.

@btucker
Created September 10, 2008 17:00
Show Gist options
  • Select an option

  • Save btucker/9952 to your computer and use it in GitHub Desktop.

Select an option

Save btucker/9952 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
=head1 NAME
Rename Parameterized files (as downloaded by wget)
=head1 DESCRIPTION
This script will find any files with question marks in them, replace them with '--' and then
do a multi-file find & replace for any references in all other files in the directory.
=cut
use strict;
use warnings;
my $base_dir = $ARGV[0];
unless ($base_dir) {
print "USAGE: ./rename_parameterized_files.pl root_directory/\n";
exit 1;
}
for my $file (`find $base_dir -name '*\?*'`) {
chomp $file;
my $new_file = $file;
$new_file =~ s/\?/--/g;
my $just_file = $file;
my $just_new_file = $new_file;
$just_file =~ s/^.*?\/([^\/]+)$/$1/;
$just_new_file =~ s/^.*?\/([^\/]+)$/$1/;
my ($escaped_file, $escaped_new_file) = (quotemeta($just_file), quotemeta($just_new_file));
print $escaped_file . "\n";
print $escaped_new_file . "\n";
system("perl -pi -e 's/$escaped_file/$escaped_new_file/' `find $base_dir -name '*.html*'`");
rename($file, $new_file);
print "RENAMING $file to $new_file\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment