Actions

Podcast Synch Batch File: Difference between revisions

From Mike Beane's Blog

(→‎Perl Script: added functionality)
Line 27: Line 27:
<pre>
<pre>
use File::Copy;
use File::Copy;
use Win32::Console::ANSI;
use Term::ANSIColor;
use Cwd;
my $dir = cwd;
$dir =~ s/\///g;
print color 'bold blue on_white';
print "Portable Player Drive is $dir\n";
print color 'reset';


my $podcastsource="C:/Documents and Settings/Mike/My Documents/My Music/Podcasts";
my $podcastsource="C:/Documents and Settings/Mike/My Documents/My Music/Podcasts";
my $podcastdest="J:/MUSIC/Podcasts";
my $podcastdest="$dir/MUSIC/Podcasts";
my $dircontents=();
my $dircontents=();
my $destdir=();
my $destdir=();


print color 'bold blue on_white';
print  "Checking source files to move to destination:\n";
print color 'reset';


print  "Checking files to move over.\n";
&move($podcastsource); #Check what is in the source and not in the dest - copy to dest
&move($podcastsource); #Check what is in the source and not in the dest - copy to dest
print "Checking files to remove from On The Go.plp\n";
 
print color 'bold blue on_white';
print "Reading On The Go.plp for files to remove from portable:\n";
print color 'reset';
 
&OnTheGo;
&OnTheGo;
print  "Checking files to remove.\n";
 
print color 'bold blue on_white';
print  "Checking for files to remove from source:\n";
print color 'reset';
 
&smash($podcastdest);  #Check what is in the dest and not in the source - del from dest
&smash($podcastdest);  #Check what is in the dest and not in the source - del from dest
 




sub OnTheGo
sub OnTheGo
     {
     {
     $LOGFILE_IN="J:/PLAYLISTS/On The Go.plp";
     $LOGFILE_IN="$dir/PLAYLISTS/On The Go.plp";
     open LOGFILE_IN, $LOGFILE_IN or die "Cannot open $LOGFILE_IN for read :$!";
     open LOGFILE_IN, $LOGFILE_IN or die "Cannot open $LOGFILE_IN for read :$!";
    #binmode LOGFILE_IN;
      
      
     while (<LOGFILE_IN>)  
     while (<LOGFILE_IN>)  
         {
         {
       
         my $tmp=$_;
         my $tmp=$_;
         $tmp =~ s/\0//g;
         $tmp =~ s/\0//g;
         $tmp =~ s/\015//g;
         $tmp =~ s/\015//g;
         $tmp =~ s/\012//g;
         $tmp =~ s/\012//g;
        #$tmp =~ s/ //;
         $tmp =~ s/HARP, /$dir\//;
         $tmp =~ s/HARP, /J:\//;
         $tmp =~ s/\\/\//g;
         $tmp =~ s/\\/\//g;
         if ($tmp =~ /J:/)
        #print "$tmp\n";
         if ($tmp =~ /$dir/)
             {
             {
            #print "$tmp";
             if ($tmp =~ m/($podcastdest)\/(.+)/)
             if ($tmp =~ m/($podcastdest)\/(.+)/)
                 {
                 {
                    my $file=$2;
                my $file=$2;
                   
                    #print "..$podcastsource/$file..\n\n";
                 if(-e "$podcastsource/$file")
                 if(-e "$podcastsource/$file")
                     {
                     {
                     $file="$podcastsource/$file";
                     $file="$podcastsource/$file";
                     unlink ($file) or warn "Cannot del $file: $!";;    
                     unlink ($file) or warn "Cannot del $file: $!";;  
                     print "$file\n";
                    print color 'bold yellow on_magenta';                   
                     print "$2\n";
                    print color 'reset';
                     }
                     }
                 }
                 }
             }
             }
         }
         }
       
        #close
        #del
       
     }
     }
sub makedest
sub makedest
     {
     {
        my $destdirectory=$destdir;
    my $destdirectory=$destdir;
        @directory2make = split(/\//, $destdirectory);
    @directory2make = split(/\//, $destdirectory);
        shift (@directory2make); #drop the first
    shift (@directory2make); #drop the first
        $directorytmp="";
    $directorytmp="";
        $directory1tmp="";
    $directory1tmp="";
      
 
        foreach $directory2make (@directory2make)
     foreach $directory2make (@directory2make)
        {
        $directory1tmp="$directory1tmp\/$directory2make";
        #print "$directory1tmp\n";
        $directorytmp="$dir$directory1tmp";
        #print "$directory1tmp\n";
        #print "$directorytmp\n";
        if(!-e $directorytmp)
             {
             {
             $directory1tmp="$directory1tmp\/$directory2make";
             print color 'bold yellow on_magenta';                  
            $directorytmp="j:$directory1tmp";
             print "Did not find $directorytmp\n";                     
            #print "$directorytmp\n";
            mkdir ("$directorytmp") or die "Cannot make $directorytmp :$!";     #incoming logs go here
             if(!-e $directorytmp)
            print "Created $directorytmp\n";     
                {
            print color 'reset';
                print "Did not find $directorytmp\n";                     
            }
                mkdir ("$directorytmp") or die "Cannot make $directorytmp :$!";     #incoming logs go here
        } # end for each
                print "Created $directorytmp\n";     
                }
            } # end for each
 
}  
}  


sub move  
sub move  
Line 122: Line 136:
         &move($_);
         &move($_);
         $dircontents=$_;
         $dircontents=$_;
        #print "$_\n";
         &createcontents($dircontents);
         &createcontents($dircontents);
         rmdir $_;
         rmdir $_;
Line 131: Line 144:
     {
     {
     opendir(DIR, "$dircontents") or die "Cannot open $dircontents to read: $!";
     opendir(DIR, "$dircontents") or die "Cannot open $dircontents to read: $!";
     @newfiles = grep(/\.mp3$/,readdir(DIR));
     @newfiles = grep(/\.mp3$/ || /\.jpg$/ ,readdir(DIR));
     closedir(DIR);
     closedir(DIR);
    #print "$dircontents\n";
     foreach $newfile (@newfiles)  
     foreach $newfile (@newfiles)  
         {
         {
       
         if ($dircontents =~ m/($podcastsource)\/(.+)/)
         if ($dircontents =~ m/($podcastsource)\/(.+)/)
             {
             {
Line 145: Line 156:
                 {
                 {
               if(!-e $destdir)  #check to see if dest exists, if not, create the dir   
               if(!-e $destdir)  #check to see if dest exists, if not, create the dir   
              {
                  {
                   &makedest($destdir);
                   &makedest($destdir);
              }
                  }
                 my $filetobecopied = "$podcastsource/$2/$newfile";
                 my $filetobecopied = "$podcastsource/$2/$newfile";
                 my $oldfile = "$podcastdest/$2/$newfile";
                 my $oldfile = "$podcastdest/$2/$newfile";
                print color 'bold yellow on_magenta';                   
                 print "Copying $newfile\n";
                 print "Copying $newfile\n";
                print color 'reset';
                 copy($filetobecopied, $oldfile) or die "File cannot be copied.";     
                 copy($filetobecopied, $oldfile) or die "File cannot be copied.";     
               
                 }
                 }
             }
             }
Line 175: Line 187:
         &smash($_);
         &smash($_);
         $dircontents=$_;
         $dircontents=$_;
        #print "$_\n";
         &destroycontents($dircontents);
         &destroycontents($dircontents);
         rmdir $_;
         rmdir $_;
      }
        }
     }
     }
      
      
Line 186: Line 197:
     @files = grep(/\.mp3$/,readdir(DIR));
     @files = grep(/\.mp3$/,readdir(DIR));
     closedir(DIR);
     closedir(DIR);
    #print "$dircontents\n";
     foreach $file (@files)  
     foreach $file (@files)  
         {
         {
       
            #print "$file\n";
         if ($dircontents =~ m/($podcastdest)\/(.+)/)
         if ($dircontents =~ m/($podcastdest)\/(.+)/)
             {
             {
Line 196: Line 206:
                 my $del="$podcastdest/$2/$file";
                 my $del="$podcastdest/$2/$file";
                 unlink ($del);       
                 unlink ($del);       
                print color 'bold yellow on_magenta';                   
                 print "$del\n";                 
                 print "$del\n";                 
                print color 'reset';
                 }
                 }
             }
             }

Revision as of 11:47, 8 June 2007

Not finding a good way to manage my podcasts on my Sansa e250 and I'm already manually dumping files from iTunes anyway (my choice).

For the meantime, here is synch.bat to del all files in the Podcasts directory on the Sansa and then xcopy everything in the iTunes Podcasts directory. It's not quick, but it works.

echo off
cls
del "g:\music\podcasts\*" /S /Q
xcopy "D:\music\Ipod\Podcasts\*.mp3" /S "g:\music\Podcasts\"


Next step I plan to write something that checks:

  • Does file exist in both places - don't copy
  • Does file exist in dest and not in source - del dest
  • Does file exist in source and not in dest - copy source
  • Do I have enough room to copy source -> dest - copy oldest files first
  • Implement create directory if not exist routine
  • Cleanup the code
  • Make dest drive setting value


Script Test

File:SubDir.jpg

Perl Script

use File::Copy;
use Win32::Console::ANSI;
use Term::ANSIColor;

use Cwd;
my $dir = cwd;
$dir =~ s/\///g;
print color 'bold blue on_white';
print "Portable Player Drive is $dir\n";
print color 'reset';

my $podcastsource="C:/Documents and Settings/Mike/My Documents/My Music/Podcasts";
my $podcastdest="$dir/MUSIC/Podcasts";
my $dircontents=();
my $destdir=();

print color 'bold blue on_white';
print  "Checking source files to move to destination:\n";
print color 'reset';

&move($podcastsource); #Check what is in the source and not in the dest - copy to dest

print color 'bold blue on_white';
print "Reading On The Go.plp for files to remove from portable:\n";
print color 'reset';

&OnTheGo;

print color 'bold blue on_white';
print  "Checking for files to remove from source:\n";
print color 'reset';

&smash($podcastdest);  #Check what is in the dest and not in the source - del from dest

  


sub OnTheGo
    {
    $LOGFILE_IN="$dir/PLAYLISTS/On The Go.plp";
    open LOGFILE_IN, $LOGFILE_IN or die "Cannot open $LOGFILE_IN for read :$!";
    
    while (<LOGFILE_IN>) 
        {
        my $tmp=$_;
        $tmp =~ s/\0//g;
        $tmp =~ s/\015//g;
        $tmp =~ s/\012//g;
        $tmp =~ s/HARP, /$dir\//;
        $tmp =~ s/\\/\//g;
        #print "$tmp\n";
        if ($tmp =~ /$dir/)
            {
            if ($tmp =~ m/($podcastdest)\/(.+)/)
                {
                my $file=$2;
                if(-e "$podcastsource/$file")
                    {
                    $file="$podcastsource/$file";
                    unlink ($file) or warn "Cannot del $file: $!";; 
                    print color 'bold yellow on_magenta';                    
                    print "$2\n";
                    print color 'reset';
                    }
                }
            }
        }
    }
	
sub makedest
    {	
    my $destdirectory=$destdir;
    @directory2make = split(/\//, $destdirectory);
    shift (@directory2make); #drop the first
    $directorytmp="";
    $directory1tmp="";

    foreach $directory2make (@directory2make)
        {
        $directory1tmp="$directory1tmp\/$directory2make";
        #print "$directory1tmp\n";
        $directorytmp="$dir$directory1tmp";
        #print "$directory1tmp\n";
        #print "$directorytmp\n";
        if(!-e $directorytmp)
            {
            print color 'bold yellow on_magenta';                    
            print "Did not find $directorytmp\n";                    
            mkdir ("$directorytmp") or die "Cannot make $directorytmp :$!";		    #incoming logs go here
            print "Created $directorytmp\n";    
            print color 'reset';
            }
        } # end for each
	} 

sub move 
    {
    my $dir = shift;
    opendir DIR, $dir or return;
    my @contents =
    map "$dir/$_",
    sort grep !/^\.\.?$/,
    readdir DIR;
    closedir DIR;
    
    foreach (@contents) 
        {
        next unless !-l && -d;
        &move($_);
        $dircontents=$_;
        &createcontents($dircontents);
        rmdir $_;
      }
    }
    
sub createcontents
    {
    opendir(DIR, "$dircontents") or die "Cannot open $dircontents to read: $!";
    @newfiles = grep(/\.mp3$/ || /\.jpg$/ ,readdir(DIR));
    closedir(DIR);
    foreach $newfile (@newfiles) 
        {
        if ($dircontents =~ m/($podcastsource)\/(.+)/)
            {
            $destdir="$podcastdest/$2";
            my $testdir="$destdir/$newfile";
            
            if(!-e $testdir)
                {
               if(!-e $destdir)  #check to see if dest exists, if not, create the dir   
                   {
                   &makedest($destdir);
                   }
                my $filetobecopied = "$podcastsource/$2/$newfile";
                my $oldfile = "$podcastdest/$2/$newfile";
                print color 'bold yellow on_magenta';                    
                print "Copying $newfile\n";
                print color 'reset';
                copy($filetobecopied, $oldfile) or die "File cannot be copied.";    
                }
            }
        }
    }



sub smash 
    {
    my $dir = shift;
    opendir DIR, $dir or return;
    my @contents =
    map "$dir/$_",
    sort grep !/^\.\.?$/,
    readdir DIR;
    closedir DIR;
    
    foreach (@contents) 
        {
        next unless !-l && -d;
        &smash($_);
        $dircontents=$_;
        &destroycontents($dircontents);
        rmdir $_;
        }
    }
    
sub destroycontents
    {
    opendir(DIR, "$dircontents") or die "Cannot open $dircontents to read: $!";
    @files = grep(/\.mp3$/,readdir(DIR));
    closedir(DIR);
    foreach $file (@files) 
        {
            #print "$file\n";
        if ($dircontents =~ m/($podcastdest)\/(.+)/)
            {
            if(!-e "$podcastsource/$2/$file")
                {
                my $del="$podcastdest/$2/$file";
                unlink ($del);      
                print color 'bold yellow on_magenta';                    
                print "$del\n";                
                print color 'reset';
                }
            }
        }
    }