sposc (2392B)
1 #!/usr/bin/perl 2 # Copyright(c) René Wagner 2019-2023 3 # https://git.sr.ht/~rwa/silkypix-helpers 4 # published under BSD 3-Clause License 5 6 use Modern::Perl '2019'; 7 use Cwd; 8 use File::Basename; 9 use File::Find::Rule; 10 use File::Spec::Functions; 11 use Getopt::Std; 12 13 $Getopt::Std::STANDARD_HELP_VERSION = 'true'; 14 15 my $VERSION = '0.5'; 16 my $PROGRAM = 'SilkyPix Orphaned Sidecar Cleaner'; 17 18 # read commandline switches 19 our $opt_l = 0; 20 21 getopts('l') or die "Invalid parameters provided! See 'sposc --help' for more details."; 22 23 # read remaining commandline args 24 # last dir will win 25 my $work_dir = getcwd; 26 foreach my $arg ( @ARGV ) 27 { 28 if ( -d $arg ) { $work_dir = $arg } 29 } 30 31 delete_files($work_dir); 32 33 exit 0; 34 35 sub delete_files 36 { 37 my ( $delete_dir ) = @_; 38 my ( $file_counter ) = 0; 39 40 say "working directory: $delete_dir"; 41 say 'action : '. (!$opt_l ? 'delete' : 'list only') ."\r\n"; 42 say 'files with missing raw:'; 43 44 # alle Dateien in allen Unterordnern *.spd *.spf suchen 45 my @files = File::Find::Rule->file->name( qr/\.sp(d|f)$/i )->in( $delete_dir ); 46 47 foreach my $file ( @files ) 48 { 49 if ( ! exists_raw_filename($file) ) 50 { 51 print $file .'...'; 52 $file_counter++; 53 if ( !$opt_l ) 54 { 55 if ( unlink $file ) 56 { 57 print 'deleted.'; 58 } 59 else 60 { 61 print " deletion failed: $!"; 62 } 63 } 64 say ''; 65 } 66 } 67 68 say "\r\n$file_counter orphaned sidecar files found."; 69 } 70 71 sub exists_raw_filename 72 { 73 my ( $original_file ) = @_; 74 75 my ( $filename, $dirs ) = fileparse($original_file); 76 # Silkypix Sidecar files reside in a "SILKYPIX_DS" folder, so we need to search in the parent folder 77 $filename =~ s/\.\d+\.sp.$//g; 78 if ( -f catfile(dirname($dirs), $filename) ) 79 { 80 return 1; 81 } 82 return 0; 83 } 84 85 sub main::VERSION_MESSAGE() 86 { 87 say $PROGRAM . ' - version ' . $VERSION; 88 say 'published under BSD 3 - Clause License'; 89 say 'Copyright(c) 2019-2023 René Wagner'; 90 say 'https://git.sr.ht/~rwa/silky-helpers'; 91 } 92 93 sub main::HELP_MESSAGE 94 { 95 say ''; 96 say 'Deletes Silkypix Sidecar files when corresponding RAW files are missing.'; 97 say 'The program searches for orphaned files in the given folder or the current working directory if no folder is given.'; 98 say ''; 99 say 'usage: sposc [options] <target folder>'; 100 say ''; 101 say 'options:'; 102 say ' -l : list-only mode - does not delete files but only lists which files would be moved'; 103 say ' --help : show this help'; 104 }