1 #!/usr/bin/env perl
2
3 # Stupid script to strip trash formatted filenames
4
5 use strict;
6 use warnings;
7
8 my @inputs = @ARGV;
9
10 sub strip_filename($) {
11
12 my $filename = shift;
13 my @chars = split("",$filename);
14 my $new_filename;
15 open(my $fh, ">>", \$new_filename) or die "Couldn't open new_filename var\n";
16 foreach my $char ( @chars ) {
17 if ( $char =~ m/\s/ ) {
18 next;
19 } elsif ( $char =~ m/\(|\)|\\|\'|\$|\!|,|\[|\]/ ) {
20 next;
21 } else {
22 print $fh $char;
23 }
24 }
25 close $fh;
26 return $new_filename;
27 }
28
29 sub construct_mv_fn($) {
30
31 my $filename = shift;
32 my @chars = split("",$filename);
33 my $new_filename;
34 open(my $fh, ">>", \$new_filename) or die "Couldn't open new_filename var\n";
35 foreach my $char ( @chars ) {
36 if ( $char eq " " ) {
37 print $fh "\\$char";
38 } else {
39 print $fh $char;
40 }
41 }
42 close $fh;
43 return $new_filename;
44
45 }
46
47 sub rename_file($$) {
48
49 my ( $old_filename, $new_filename ) = @_;
50 my $mv_filename = construct_mv_fn($old_filename);
51 my $cmd = "mv '$mv_filename' $new_filename";
52 print "$cmd\n";
53 #system("$cmd");
54
55 }
56
57 foreach my $input ( @inputs ) {
58 if ( ! -f $input ) {
59 print "File at path $input doesn't look like a file\n";
60 exit 1;
61 }
62 print "$input\n";
63 my $new_filename = strip_filename($input);
64 print "$new_filename\n";
65 rename_file($input,$new_filename);
66 }