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