💾 Archived View for gmn.clttr.info › sources › photo-helpers.git › tree › sposc.txt captured on 2023-04-26 at 13:54:12.

View Raw

More Information

⬅️ Previous capture (2023-01-29)

-=-=-=-=-=-=-

#!/usr/bin/perl
# Copyright(c) René Wagner 2019-2020
# https://src.clttr.info/rwa/photo-helpers
# published under BSD 3-Clause License

use Modern::Perl '2019';
use Cwd;
use File::Basename;
use File::Find::Rule;
use File::Spec::Functions;
use Getopt::Std;

$Getopt::Std::STANDARD_HELP_VERSION = 'true';

my $VERSION = '0.5';
my $PROGRAM = 'SilkyPix Orphaned Sidecar Cleaner';

# read commandline switches
our $opt_l = 0;

getopts('l') or die "Invalid parameters provided! See 'sposc --help' for more details.";

# read remaining commandline args
# last dir will win
my $work_dir = getcwd;
foreach my $arg ( @ARGV ) 
{
	if ( -d $arg ) { $work_dir = $arg }
}

delete_files($work_dir);

exit 0;

sub delete_files
{
	my ( $delete_dir ) = @_;
	my ( $file_counter ) = 0;

	say "working directory: $delete_dir";
	say 'action           : '. (!$opt_l ? 'delete' : 'list only') ."\r\n";
	say 'files with missing raw:';

	# alle Dateien in allen Unterordnern *.spd *.spf suchen
	my @files = File::Find::Rule->file->name( qr/\.sp(d|f)$/i )->in( $delete_dir );
	
	foreach my $file ( @files )
	{
		if ( ! exists_raw_filename($file) )
		{
			print $file .'...';
			$file_counter++;
			if ( !$opt_l )
			{
				if ( unlink $file )
				{
					print 'deleted.';
				}
				else
				{
					print " deletion failed: $!";
				} 
			}
			say '';
		}
	}
	
	say "\r\n$file_counter orphaned sidecar files found.";
}

sub exists_raw_filename
{
	my ( $original_file ) = @_;
	
	my ( $filename, $dirs ) = fileparse($original_file);
	# Silkypix Sidecar files reside in a "SILKYPIX_DS" folder, so we need to search in the parent folder
	$filename =~ s/\.\d+\.sp.$//g;
	if ( -f catfile(dirname($dirs), $filename) )
	{
		return 1;
	}
	return 0;
}

sub main::VERSION_MESSAGE()
{
	say $PROGRAM . ' - version ' . $VERSION;
	say 'published under BSD 3 - Clause License';
	say 'Copyright(c) 2019-2020 René Wagner';
	say 'https://git.sr.ht/~rwa/photo-helpers';
}

sub main::HELP_MESSAGE
{
	say '';
	say 'Deletes Silkypix Sidecar files when corresponding RAW files are missing.';
    say 'The program searches for orphaned files in the given folder or the current working directory if no folder is given.';
	say '';
	say 'usage: sposc [options] <target folder>';
	say '';
	say 'options:';
	say '    -l       : list-only mode - does not delete files but only lists which files would be moved';
	say '    --help   : show this help';
}