Processing complete! Number of files created: 492 File type counts (sorted from greatest to least): png: 345 jpg: 67 jpeg: 61 mp4: 18 gif: 1 (base) Merrills-Mac-mini:Documents mymac$ ./3.list.pl --target /Users/mymac/Pictures/LanX Created target directory: /Users/mymac/Pictures/LanX Starting to process files in /Users/mymac/Documents/Suite... Skipping: . (not a file) Skipping file: .DS_Store (not an image or video) #### #!/usr/bin/perl # ---------------------------------------------------------------------------- # Script to organize and convert image files in a source directory # Authors: Aldebaran and ChatGPT. 09-06-2024 # For educational purposes only. Authors assume no liability. # ---------------------------------------------------------------------------- # This script processes image and video files from a specified source directory. # It sorts files into directories based on their modification date (year, month, day), # converts HEIC files to JPG, and renames files to a standardized format. # It ensures no file is overwritten and removes the original files after processing. # ---------------------------------------------------------------------------- use strict; use warnings; use v5.30; # Aldebaran’s “right perl” use File::Find; use File::Path qw(make_path); use File::Copy qw(move); use POSIX qw(strftime); use Getopt::Long; # Default source and target directories my $source_dir = '/Users/mymac/Documents/Suite'; my $target_dir = '/Users/mymac/Documents/Test1'; # Get source and target directories from CLI arguments GetOptions( "source=s" => \$source_dir, "target=s" => \$target_dir, ) or die "Error in command line arguments\n"; # Check if the source directory exists unless (-d $source_dir) { die "Source directory $source_dir does not exist.\n"; } # Create the target directory if it doesn't exist unless (-d $target_dir) { make_path($target_dir) or die "Failed to create target directory $target_dir: $!\n"; print "Created target directory: $target_dir\n"; } # Hash to store file type counts and total file count my %file_types; my $file_count = 0; $DB::single = 1; # Subroutine to process each file sub process_file { # Only process files if (-f $_) { # Extract file extension and convert to lowercase my ($ext) = $_ =~ /\.([^.]+)$/; $ext = lc $ext if defined $ext; # Convert to lowercase # Only process specific file types if (defined $ext && $ext =~ /^(jpg|jpeg|png|gif|bmp|tiff|heic|mp4|mov)$/) { print "Processing file: $_\n"; $DB::single = 1; my $file_path = $File::Find::name; print "File path: $file_path\n"; # Get file modification time and format it my $mod_time = (stat($file_path))[9]; my $date = strftime "%Y_%m_%d", localtime($mod_time); my ($year, $month, $day) = split('_', $date); # Create destination directory based on file date my $dest_dir = "$target_dir/$year/$month"; unless (-d $dest_dir) { make_path($dest_dir) or die "Failed to create destination directory $dest_dir: $!\n"; print "Created destination directory: $dest_dir\n"; } # Initialize file counter for the current date my $count = 1; my $new_file_name; # Convert HEIC files to JPG if ($ext eq 'heic') { $new_file_name = "${year}_${month}_${day}_$count.jpg"; # Ensure unique file name while (-e "$dest_dir/$new_file_name") { $count++; $new_file_name = "${year}_${month}_${day}_$count.jpg"; } my $converted_file_path = "$dest_dir/$new_file_name"; my $convert_command = "heif-convert $file_path $converted_file_path"; system($convert_command) == 0 or die "Failed to convert $file_path: $!\n"; unlink $file_path or warn "Failed to delete original file $file_path: $!\n"; print "Converted and moved: $file_path -> $converted_file_path\n"; # Increment file type count $file_types{'jpg'}++; } else { $new_file_name = "${year}_${month}_${day}_$count.$ext"; # Ensure unique file name while (-e "$dest_dir/$new_file_name") { $count++; $new_file_name = "${year}_${month}_${day}_$count.$ext"; } move($file_path, "$dest_dir/$new_file_name") or die "Failed to move file $file_path: $!\n"; unlink $file_path or warn "Failed to delete original file $file_path: $!\n"; print "Moved and renamed: $file_path -> $dest_dir/$new_file_name\n"; # Increment file type count $file_types{$ext}++; } # Increment total file count $file_count++; } else { print "Skipping file: $_ (not an image or video)\n"; } } else { print "Skipping: $_ (not a file)\n"; } } # Find and process files in the source directory print "Starting to process files in $source_dir...\n"; find(\&process_file, $source_dir); # Print the summary of processed files print "Processing complete! Number of files created: $file_count\n"; print "File type counts (sorted from greatest to least):\n"; # Sort and print the file type counts foreach my $type (sort { $file_types{$b} <=> $file_types{$a} } keys %file_types) { print "$type: $file_types{$type}\n"; } #### (base) Merrills-Mac-mini:2023 mymac$ cd 06 (base) Merrills-Mac-mini:06 mymac$ ll total 799096 drwxr-xr-x 4 mymac staff 128 Jun 9 23:38 ./ drwxr-xr-x 12 mymac staff 384 Jun 9 23:41 ../ -rw-r--r--@ 1 mymac staff 45958033 Jun 21 2023 2023_06_21_1.mov -rw-r--r--@ 1 mymac staff 363174818 Jun 21 2023 2023_06_21_2.mov (base) Merrills-Mac-mini:06 mymac$