Moved and renamed: /home/fritz/Pictures/6.pics/2022-12-08-220201009.mp4 -> /media/hogan/175F-DC61/Organized_Pics/2022/12/2022_12_08_5.mp4 Skipping: Wallpapers (not a file) Processing file: Screenshot from 2022-10-16 16-58-24.png File path: /home/fritz/Pictures/Wallpapers/Screenshot from 2022-10-16 16-58-24.png Failed to delete original file /home/fritz/Pictures/Wallpapers/Screenshot from 2022-10-16 16-58-24.png: No such file or directory Moved and renamed: /home/fritz/Pictures/Wallpapers/Screenshot from 2022-10-16 16-58-24.png -> /media/hogan/175F-DC61/Organized_Pics/2022/10/2022_10_16_13.png Processing complete! Number of files created: 960 fritz@laptop:~/Documents$ #### #!/usr/bin/perl use strict; use warnings; use File::Find; use File::Path qw(make_path); use File::Copy qw(move); use POSIX qw(strftime); # Define the source and target directories my $source_dir = '/home/fritz/Pictures'; my $target_dir = '/media/hogan/175F-DC61/Organized_Pics'; # 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 my %file_types; my $file_count = 0; # Subroutine to process each file sub process_file { if (-f $_) { my ($ext) = $_ =~ /\.([^.]+)$/; $ext = lc $ext if defined $ext; # Convert to lowercase if (defined $ext && $ext =~ /^(jpg|jpeg|png|gif|bmp|tiff|heic|mp4)$/) { print "Processing file: $_\n"; my $file_path = $File::Find::name; print "File path: $file_path\n"; my $mod_time = (stat($file_path))[9]; my $date = strftime "%Y_%m_%d", localtime($mod_time); my ($year, $month, $day) = split('_', $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"; } my $count = 1; my $new_file_name; if ($ext eq 'heic') { $new_file_name = "${year}_${month}_${day}_$count.jpg"; 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"; } else { $new_file_name = "${year}_${month}_${day}_$count.$ext"; 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"; } $file_count++; } else { print "Skipping file: $_ (not an image)\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 "Processing complete! Number of files created: $file_count\n";