| 1 | #!/usr/bin/env perl | ||||||||||||||
| 2 | # PODNAME: cavaspazi | ||||||||||||||
| 3 | # ABSTRACT: a simple script to remove spaces from filenames or file contents | ||||||||||||||
| 4 | |||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | ▶ | use Getopt::Long; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 3 | ▶ | use Pod::Usage; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 3 | ▶ | use File::Basename; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 3 | ▶ | use File::Spec; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 3 | my $rename = 0; | |||||||||||||
| 12 | 3 | my $substitute = 0; | |||||||||||||
| 13 | 3 | my $dry_run; | |||||||||||||
| 14 | my $verbose; | ||||||||||||||
| 15 | 3 | my $help; | |||||||||||||
| 16 | 3 | GetOptions( | |||||||||||||
| 17 | 'rename|r' => \$rename, | ||||||||||||||
| 18 | 'substitute|s' => \$substitute, | ||||||||||||||
| 19 | 'dry-run|n' => \$dry_run, | ||||||||||||||
| 20 | 'verbose' => \$verbose, | ||||||||||||||
| 21 | 'h|help' => \$help, | ||||||||||||||
| 22 | ); | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 3 | ▶ | pod2usage({-exitval => 0, -verbose => 2}) if $help; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | |||||||||||||||
| 26 | 3 | my $cnt = 0; | |||||||||||||
| 27 | 3 | my $tot = scalar @ARGV; | |||||||||||||
| 28 | 3 | for my $file (@ARGV) { | |||||||||||||
| 29 | 5 | $cnt++; | |||||||||||||
| 30 | 5 | my $basename = basename($file); | |||||||||||||
| 31 | 5 | my $dirname = dirname($file); | |||||||||||||
| 32 | # check if file exists | ||||||||||||||
| 33 | 5 | ▶ | if (!-e $file) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | 0 | print STDERR "$cnt/$tot File $file does not exist. Skipping...\n"; | |||||||||||||
| 35 | 0 | next; | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | 5 | ▶ | if ($verbose) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | 3 | ▶ | my $type = -d $file ? 'directory' : 'file'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | 3 | print STDERR "$cnt/$tot Processing $dirname -> $basename [$type]...\n" | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | 5 | ▶ | if ($rename) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 42 | |||||||||||||||
| 43 | 4 | my $new_file = $basename; | |||||||||||||
| 44 | 4 | $new_file =~ s/ /_/g; | |||||||||||||
| 45 | 4 | my $dest = File::Spec->catfile($dirname, $new_file); | |||||||||||||
| 46 | 4 | ▶ | if ($dry_run) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | 2 | print "#mv \"$file\" \"$dest\"\n"; | |||||||||||||
| 48 | } else { | ||||||||||||||
| 49 | 2 | ▶ | rename "$file", "$dest" or die "Can't rename $file to $dest: $!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | } | ||||||||||||||
| 51 | } | ||||||||||||||
| 52 | elsif ($substitute) { | ||||||||||||||
| 53 | 1 | ▶ | if (-d $file) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | 0 | ▶ | print STDERR "Skipping: substitute only works with files...\n" if ($verbose); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 0 | next; | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | 1 | ▶ | open my $fh, '<', $file or die "Can't open $file: $!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 1 | my $content = do { local $/; <$fh> }; | |||||||||||||
| 59 | 1 | close $fh; | |||||||||||||
| 60 | 1 | $content =~ s/ /_/g; | |||||||||||||
| 61 | 1 | ▶ | open $fh, '>', $file or die "Can't open $file: $!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 1 | print $fh $content; | |||||||||||||
| 63 | 1 | print STDERR $content; | |||||||||||||
| 64 | 1 | close $fh; | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | else { | ||||||||||||||
| 67 | 0 | die "You must specify either the -r or -s option"; | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | __END__ | ||||||||||||||