silkypix-helpers

scripts for managing sidecar files created by SilkyPix Developer Studio
git clone https://git.clttr.info/silkypix-helpers.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

spmvsc (2796B)


      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 Getopt::Std;
     11 
     12 $Getopt::Std::STANDARD_HELP_VERSION = 'true';
     13 
     14 my $VERSION = '0.6';
     15 my $PROGRAM = 'SilkyPix Multiversion Sidecar Cleaner';
     16 
     17 # read commandline switches
     18 our $opt_l = 0;
     19 
     20 getopts('l') or die "Invalid parameters provided! See 'spmvsc --help' for more details.";
     21 
     22 # read remaining commandline args
     23 # last dir will win
     24 my $work_dir = getcwd;
     25 foreach my $arg ( @ARGV ) 
     26 {
     27 	if ( -d $arg ) { $work_dir = $arg }
     28 }
     29 
     30 delete_files($work_dir);
     31 
     32 exit 0;
     33 
     34 sub delete_files
     35 {
     36 	my ( $dir ) = @_;
     37 	my ( $file_counter ) = 0;
     38 
     39 	say "working directory: $dir";
     40 	say 'action           : '. (!$opt_l ? 'delete' : 'list only') . "\r\n";
     41 	say 'files with newer versions:';
     42 
     43 	# alle Dateien in allen Unterordnern *.spd *.spf suchen
     44 
     45 	my @files = File::Find::Rule->file->name( qr/\.sp(d|f)$/i )->in( $dir );
     46 	
     47 	foreach my $file ( @files )
     48 	{
     49 		if ( exist_newer_file($file, @files) )
     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 duplicate file versions found.";
     69 }
     70 
     71 sub exist_newer_file
     72 {
     73 	my ( $original_file, @files_hash ) = @_;
     74 	
     75 	if ( -f $original_file )
     76 	{
     77 		# build the regex to find all versions of this file
     78 		my @original_file_parts = split /\./, basename($original_file);
     79 
     80 		# don't handle files that doesn't have a version
     81 		if ( scalar @original_file_parts < 4 )
     82 		{
     83 			return 0;
     84 		}
     85 
     86 		my $filefinder_regex = "$original_file_parts[0]\\.$original_file_parts[1]\\.[0-9]+\\.$original_file_parts[3]";
     87 		my @version_files = File::Find::Rule->file->name( qr/$filefinder_regex/i )->in( dirname($original_file) );
     88 		
     89 		foreach my $version_file ( @version_files )
     90 		{
     91 			my @version_file_parts = split /\./, $version_file;
     92 			if ( $version_file_parts[2] > $original_file_parts[2] ) { return 1; }
     93 		}
     94 	}
     95 	
     96 	return 0;
     97 }
     98 
     99 sub main::VERSION_MESSAGE()
    100 {
    101 	say $PROGRAM . ' - version ' . $VERSION;
    102 	say 'published under BSD 3 - Clause License';
    103 	say 'Copyright(c) 2019-2023 René Wagner';
    104 	say 'https://git.sr.ht/~rwa/silkypix-helpers';
    105 }
    106 
    107 sub main::HELP_MESSAGE
    108 {
    109 	say '';
    110 	say 'Deletes Silkypix Sidecar files when newer versions of the files are available.';
    111 	say 'This usually happens when a file is opened in a newer version of Silkypix, as the sidecar files are version specific.';
    112 	say '';
    113 	say 'usage: spmvsc [options] <target folder>';
    114 	say '';
    115 	say 'options:';
    116 	say '    -l     : list-only mode - does not delete files but only lists which files would be moved';
    117 	say '    --help : show this help';
    118 }