#!/usr/bin/perl # # fixfiles.pl by michael williams (gash@maverick.org) (http://gash.freeshell.org) # # This mess was written to defunkify filenames... it converts alpha characters # to lowercase, removes spaces and removed obfuscated characters that have no # business in a filename. # # A quick warning - this script can seriously damage system files # if used incorrectly. Only use it on your personal data, not on # system files! # use strict; use File::Find; my $ver = "1.0 Wed Feb 7 22:35:22 EST 2001"; my $debug = 0; my $test = 0; my $overwrite = 0; my $verbose = 0; my $root = 0; my $count = 0; my $skipped = 0; my $overwritten = 0; my $shift = 0; my $filter = "."; my $temp; while (1) { $temp = @ARGV[0]; if ($temp =~ /^-(f|-filter)$/i) { shift(@ARGV); $filter = shift(@ARGV); } elsif ($temp =~ /^-(?:h|-help)$/i) { &show_help(); } elsif ($temp =~ /^-(o|-overwrite)$/i) { shift(@ARGV); $overwrite = 1; } elsif ($temp =~ /^-(r|-root)$/i) { shift(@ARGV); $root = 1; } elsif ($temp =~ /^-(v|-verbose)$/i) { shift(@ARGV); $verbose = 1; } elsif ($temp =~ /^-(t|-test)$/i) { shift(@ARGV); $test = 1; } else { last; } } &show_help() if (@ARGV < 1); foreach $temp (@ARGV) { print "arg: $temp\n\n" if ($debug); &show_help() if ($temp =~ /^-/); die "\nRefusing to run from /, use -r option if this is what you really want!\n\n" if ($temp eq "/" && ! $root); die "\nUnable to write to $temp ... quitting\n\n" if ( ! -w $temp); } if ($debug) { print "O: $overwrite, V: $verbose, R: $root, F: $filter T: $test\n"; exit; } print "\n$0 $ver started...\n\n"; find(\&process_file, @ARGV); print "\nDone. Stats: Fixed: $count, Skipped: $skipped, Overwritten: $overwritten\n\n"; exit 0; sub process_file { my ($orig_name, $new_name,@args); $orig_name = $new_name = $_; return if ($orig_name !~ /${filter}/i); print "Processing: \"$File::Find::name\"\n" if ($verbose); $new_name =~ tr/A-Z/a-z/; $new_name =~ s/[^a-z0-9.\-_]+/_/g; # fix extensions $new_name =~ s/\.mpe?$/\.mpg/; # did the filename change? return if ("$orig_name" eq "$new_name"); if (! -w $orig_name) { print "Skipping: \"$File::Find::name\", cannot rename\n"; $skipped++; return; } if (-f $new_name) { if ($overwrite) { if (! -w $new_name) { print "Skipping: \"$File::Find::name\", cannot unlink \"$new_name\"\n"; $skipped++; return; } else { print "Overwriting: \"$File::Find::name\" -> \"$new_name\"\n"; $overwritten++; } } else { print "Skipping: \"$File::Find::name\", destination \"$new_name\" already exists\n"; $skipped++; return; } } else { print "Converting: \"$File::Find::name\" -> \"$new_name\"\n"; } rename($orig_name,$new_name) || die "Couldn't rename $orig_name to $new_name: $!\n" if (! $test); $count++; } sub show_help { print "\nFixFiles.pl v$ver\n\nUsage: $0 [-f ] [-h] [-o] [-r] [-v] ...\n"; print " -f --filter mangle this file type only\n"; print " -h --help display this message\n"; print " -o --overwrite overwrite destination files\n"; print " -r --root allow run from root directory (dangerous)\n"; print " -t --test test mode, evaluate files, but do not fix them\n"; print " -v --verbose verbose mode\n\n"; exit 0; }