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

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::Copy;
use File::Find::Rule;
use File::Spec::Functions;
use Getopt::Std;

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

my $VERSION = '0.6';
my $PROGRAM = 'JpegDivider';

# read commandline switches
our $opt_l = 0;
our $opt_f = 'JPEG';
our $opt_e = 'jpe{0,1}g';

getopts('lf:e:') or die "Invalid parameters provided! See 'jd --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 }
}

move_files($work_dir);

exit 0;

sub move_files
{
	my ( $dir ) = @_;
	my $move_counter = 0;

	say "working directory: $dir";
	say 'action           : '. (!$opt_l ? 'move' : 'list only');
	say "subfolder        : $opt_f";
	say "file ext         : $opt_e";
    say '';
	
	my $destination_dir = catdir($dir, $opt_f);
	if ( !$opt_l && ! -d $destination_dir )
	{
		mkdir $destination_dir or die "Could not create destination dir $_ : $!";
	}
	
	my @files = File::Find::Rule->file()->name( qr/\.$opt_e$/i )->maxdepth(1)->in( $dir );
	
	foreach my $file ( @files )
	{
		print $file .'...';
		$move_counter++;
		if ( !$opt_l )
		{
			if ( move($file, catfile($destination_dir, basename($file)) ) )
			{ 
				print "moved.";
			}
			else
			{
				print "move failed: $!";
			}
		}
		say '';
	}
	
	say "\r\nfound $move_counter jpeg files to subfolder.";
}

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 'Moves JPEG files to a designated subfolder, by default "JPEG"';
	say '';
	say 'usage: jd [options] <target folder>';
	say '';
	say 'options:';
	say '   -l          : list-only mode - does not move files but only lists which files would be moved';
	say '   -f <folder> : use the given subfolder instead of "JPEG"';
    say '   -e <ext>    : override the converted file extenion (case-insensitive), defaults to "jp(e)g"';
    say '                 Perl-compatible regular expressions allowed';
	say '   --help      : show this help';
}