photo-helpers

Unnamed repository; edit this file 'description' to name the repository.
git clone git://code.clttr.info/photo-helpers.git
Log | Files | Refs | README | LICENSE

commit d452d3773fb23df79a7573dc26d379eb9713a0b4
parent 01ddff3a8bd9cc92d077f297341db191377dd43c
Author: René Wagner <apollo@rw-net.de>
Date:   Sun, 29 Sep 2019 21:33:23 +0200

remove older versions of sidecar files
works till version 9 of Silkypix

Diffstat:
Mmultiversion_sidecar_cleaner.pl | 48+++++++++++++++++++++++++++++++++++++-----------
1 file changed, 37 insertions(+), 11 deletions(-)

diff --git a/multiversion_sidecar_cleaner.pl b/multiversion_sidecar_cleaner.pl @@ -27,8 +27,6 @@ foreach my $arg ( @ARGV ) } } -print "working directory: $delete_dir \r\n"; - main($delete_dir, $delete_flag); exit 0; @@ -38,6 +36,10 @@ sub main my ( $dir, $delete ) = @_; my ( $delcounter ) = 0; + print "working directory: $delete_dir \r\n"; + print 'action : '. ($delete_flag ? 'delete' : 'list only') . "\r\n \r\n"; + print "files with newer versions:\r\n"; + # alle Dateien in allen Unterordnern *.spd *.spf suchen my $rule = File::Find::Rule->new; $rule->file; @@ -47,33 +49,57 @@ sub main my $file; foreach $file ( @files ) { - my $nextfile = get_next_filename($file); - - if ( -f $nextfile ) + if ( exist_newer_file($file) ) { print $file .'...newer file found!'; $delcounter++; if ( $delete ) { - unlink $file ? print ' File deleted' : print ' Deletion failed'; + unlink $file or warn " Deletion failed: $!"; } print "\r\n"; } } - print 'Found '. $delcounter .' duplicate file versions.'; + print "\r\nfound $delcounter duplicate file versions. \r\n"; } -sub get_next_filename +sub exist_newer_file { my ( $original_file ) = @_; if ( -f $original_file ) { - my ( $filename, $dirs, $suffix ) = fileparse($original_file, qr/\.sp\w/); - my ( $file, $version ) = split /\.([^\.]+)$/, $filename; - return $dirs . $file .'.'. ($version + 1) . $suffix; + # build the regex for newer versions + # the version must be atleast 1 above the current version + my $filename = basename($original_file); + my @fileparts = split /\./, $filename; + + my $version_regex; + # HACK: file versions greater 8 will not be checked as the regex can't handle that atm + if ( $fileparts[2] > 8 ) + { + return 0; + } + else + { + $version_regex = '['. ($fileparts[2] + 1) .'...9]'; + } + + my $filefinder_regex = "$fileparts[0].$fileparts[1].$version_regex.$fileparts[3]"; + + my $versions_rule = File::Find::Rule->new; + $versions_rule->file; + $versions_rule->name( $filefinder_regex ); + my @files = $versions_rule->in( dirname($original_file) ); + + if ( scalar @files ) + { + return 1; + } } + + return 0; } sub show_help