matthiasxc / PlexClassicCartoons

Scripts for organizing collections of classic cartoons (Disney, Looney Tunes) properly into Plex
MIT License
37 stars 7 forks source link

FOR Loop for Directory Creation and File Moves #4

Open stevenffrench opened 3 years ago

stevenffrench commented 3 years ago

I recommend using a FOR loop for the directory creation and file moves. It's far more efficient. Here's a version with that implemented.

mushu999 commented 3 years ago

Here's a Perl script to rename them - I didn't care if they were in perfect order in each directory, I just needed them to be indexed by Plex, so any purists won't be happy. Also, I renamed the base directory to "LooneyTunes" and thus the file names will all start with that followed by S[Year]E[counter] in each subdirectory. Make sure you change the path in the "for" command to your own Plex TV shows path!

# to run, drop to a CMD prompt and get into your Looney Tunes base directory
#   while in the Looney Tunes base directory you iterate thru each subdirectory and run the script
#   for /d %x in ("C:\plex\tv-shows\LooneyTunes\Season*") do cd "%x" && perl ..\rf.pl

use File::Basename;
use Cwd qw();
use strict;

opendir(DIR,'.');
my $cd = Cwd::cwd();        # get current directory
my $c=0;                    # init episode counter
while(my $fil=readdir(DIR)) {
    next if -d $fil;            # ignore subdirectories (so only works 1 level down)
    (my $b, my $d, my $e) = fileparse($fil,'\..+');
    my $w = substr($cd, -4);        # grab last 4 chars of current directory name
    next if ($w<1 or $w>1000000);   # cheap test for if it is a number (makes sure we're in a Year)
    next if ($fil =~ m/^LooneyTunes/);  # ignore already renamed files (change to match your system)
    $c++;
    my $ep = sprintf("%02d",$c);
    my $newfil = "LooneyTunes - S$w" . "E$ep - $fil";       # Change LooneyTunes to match your system
    print "Renaming $fil to $newfil\n\n";
    rename($fil,$newfil) or warn "Couldn't rename file: $!\n";
}
closedir(DIR);
exit;