in reply to Re: a nifty utility script from chatgpt
in thread a nifty utility script from chatgpt
Ok. We beefed that up. I added a use statement for my perfect perl, which is 5.30. I don't know what the current one is, and maybe we should slow down the advance of the number, since it's the only one this language gets, as the legacy language that it is. There will be no perl 7, and perl 6 turned into Raku. So we better start conserving 5.
parametrization of hardcoded variables by CLII didn't get what you meant, but I asked chatgpt, and they hooked it up. You know it wasn't me if it works.
a test suit for your requirementsI devised a couple. On one I changed the target path:
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)
I set a couple breakpoints and went over it in the debugger and liked what I saw.
Current source:
#!/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 (ye +ar, month, day), # converts HEIC files to JPG, and renames files to a standardized form +at. # It ensures no file is overwritten and removes the original files aft +er 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 destinat +ion 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.j +pg"; } my $converted_file_path = "$dest_dir/$new_file_name"; my $convert_command = "heif-convert $file_path $conver +ted_file_path"; system($convert_command) == 0 or die "Failed to conver +t $file_path: $!\n"; unlink $file_path or warn "Failed to delete original f +ile $file_path: $!\n"; print "Converted and moved: $file_path -> $converted_f +ile_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 "F +ailed to move file $file_path: $!\n"; unlink $file_path or warn "Failed to delete original f +ile $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 %f +ile_types) { print "$type: $file_types{$type}\n"; }
I'm not gonna say that I haven't lost files in the testing. What I have now looks all lined up, concise, clean.
(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$
Cheers,
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: a nifty utility script from chatgpt
by hippo (Archbishop) on Jun 10, 2024 at 08:19 UTC |