jd (2325B)
1 #!/usr/bin/perl 2 # Copyright(c) René Wagner 2019-2023 3 # https://git.sr.ht/~rwa/art-helpers 4 # published under BSD 3-Clause License 5 6 use Modern::Perl '2019'; 7 use Cwd; 8 use File::Basename; 9 use File::Copy; 10 use File::Find::Rule; 11 use File::Spec::Functions; 12 use Getopt::Std; 13 14 $Getopt::Std::STANDARD_HELP_VERSION = 'true'; 15 16 my $VERSION = '0.6'; 17 my $PROGRAM = 'JpegDivider'; 18 19 # read commandline switches 20 our $opt_l = 0; 21 our $opt_f = 'JPEG'; 22 our $opt_e = 'jpe{0,1}g'; 23 24 getopts('lf:e:') or die "Invalid parameters provided! See 'jd --help' for more details."; 25 26 # read remaining commandline args 27 # last dir will win 28 my $work_dir = getcwd; 29 foreach my $arg ( @ARGV ) 30 { 31 if ( -d $arg ) { $work_dir = $arg } 32 } 33 34 move_files($work_dir); 35 36 exit 0; 37 38 sub move_files 39 { 40 my ( $dir ) = @_; 41 my $move_counter = 0; 42 43 say "working directory: $dir"; 44 say 'action : '. (!$opt_l ? 'move' : 'list only'); 45 say "subfolder : $opt_f"; 46 say "file ext : $opt_e"; 47 say ''; 48 49 my $destination_dir = catdir($dir, $opt_f); 50 if ( !$opt_l && ! -d $destination_dir ) 51 { 52 mkdir $destination_dir or die "Could not create destination dir $_ : $!"; 53 } 54 55 my @files = File::Find::Rule->file()->name( qr/\.$opt_e$/i )->maxdepth(1)->in( $dir ); 56 57 foreach my $file ( @files ) 58 { 59 print $file .'...'; 60 $move_counter++; 61 if ( !$opt_l ) 62 { 63 if ( move($file, catfile($destination_dir, basename($file)) ) ) 64 { 65 print "moved."; 66 } 67 else 68 { 69 print "move failed: $!"; 70 } 71 } 72 say ''; 73 } 74 75 say "\r\nfound $move_counter jpeg files to subfolder."; 76 } 77 78 sub main::VERSION_MESSAGE() 79 { 80 say $PROGRAM . ' - version ' . $VERSION; 81 say 'published under BSD 3 - Clause License'; 82 say 'Copyright(c) 2019-2023 René Wagner'; 83 say 'https://git.sr.ht/~rwa/art-helpers'; 84 } 85 86 sub main::HELP_MESSAGE 87 { 88 say ''; 89 say 'Moves JPEG files to a designated subfolder, by default "JPEG"'; 90 say ''; 91 say 'usage: jd [options] <target folder>'; 92 say ''; 93 say 'options:'; 94 say ' -l : list-only mode - does not move files but only lists which files would be moved'; 95 say ' -f <folder> : use the given subfolder instead of "JPEG"'; 96 say ' -e <ext> : override the converted file extenion (case-insensitive), defaults to "jp(e)g"'; 97 say ' Perl-compatible regular expressions allowed'; 98 say ' --help : show this help'; 99 }