Actions

Podcast Synch Batch File: Difference between revisions

From Rabbi Blog

mNo edit summary
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Background=
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).
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.
<pre>
echo off
cls
del "g:\music\podcasts\*" /S /Q
xcopy "D:\music\Ipod\Podcasts\*.mp3" /S "g:\music\Podcasts\"
</pre>


Next step I plan to write something that checks:
Next step I plan to write something that checks:
* Cleanup the code
* Make a playlist (by directory or by group?)
* <s>Does file exist in both places - don't copy</s>
* <s>Does file exist in both places - don't copy</s>
* <s>Does file exist in dest and not in source - del dest</s>
* <s>Does file exist in dest and not in source - del dest</s>
* <s>Does file exist in source and not in dest - copy source</s>
* <s>Does file exist in source and not in dest - copy source</s>
* Do I have enough room to copy source -> dest - copy oldest files first
* <s>Implement create directory if not exist routine</s>
* <s>Implement create directory if not exist routine</s>
* Cleanup the code
* <s>Read the Sansa On The Go Playlist and delete files that are found</s>
* Make dest drive setting value
* <s>Make dest drive setting value</s>
* '''REMOVED''' <s>Check ID3 tag and set if blank</s> - see [[&johncdvorak]]




=Script Test=
=Script Test=
Pre-removal of the ANSI Win32 Color references.
[[Image:SubDir.jpg]]
[[Image:SubDir.jpg]]


=Perl Script=
=Perl Script=
<pre>
 
use File::Copy;
Removed the MP3:: info/tag routines and also the Win32 references.  That should make it cross platform.
 
<pre>#!/usr/local/bin/perl
use warnings;
use strict;
 
use File::Copy;     #http://perldoc.perl.org/File/Copy.html
use Cwd;            #http://perldoc.perl.org/Cwd.html
 
#################################################################
#######################USER VARIABLES############################
#################################################################


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";
 
#################################################################
 
#################################################################
########################System Variables ########################
#################################################################
 
my $dircontents=();
my $dircontents=();
my $destdir=();
my $destdir=();
my @newfile=();
my $newfile=();
my @newfiles=();
my @files=();
my $file=();
my $directorytmp=();
my $directory1tmp=();
my $directory2make=();
my @directory2make=();


###########################################################################
# Run the script from the portable and it will determine its current path #
###########################################################################
my $dir = cwd;
$dir =~ s/\///g;
my $LOGFILE_IN="$dir/PLAYLISTS/On The Go.plp";
my $podcastdest="$dir/MUSIC/Podcasts";


print "Checking files to move over.\n";
#################################################################
&move($podcastsource); #Check what is in the source and not in the dest - copy to dest
######################## Start Processing #######################
print "Checking files to remove from On The Go.plp\n";
#################################################################
 
print "#################################################################\n";
print "Portable Player Drive is $dir\n";
print "On The Go.plp set to $LOGFILE_IN\n";
print "Music directory is at $podcastdest\n";
print "#################################################################\n\n";
print "Reading On The Go.plp for files to remove from portable:\n";
&OnTheGo;
&OnTheGo;
print  "Checking files to remove.\n";
print "#################################################################\n\n";
print  "Checking source files to move to destination:\n";
&movefiles($podcastsource); #Check what is in the source and not in the dest - copy to dest
print "#################################################################\n\n";
print  "Checking for files removed from source:\n";
&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
print "#################################################################\n\n";
print "Done\n";
#################################################################
######################### End Processing ########################
#################################################################


#################################################################
######################### Subroutines ########################
#################################################################


sub OnTheGo
sub OnTheGo
     {
     {
     $LOGFILE_IN="J:/PLAYLISTS/On The Go.plp";
     if(-e "$LOGFILE_IN")
    open LOGFILE_IN, $LOGFILE_IN or die "Cannot open $LOGFILE_IN for read :$!";
    #binmode LOGFILE_IN;
   
    while (<LOGFILE_IN>)  
         {
         {
          
         open LOGFILE_IN, $LOGFILE_IN or warn "Cannot open $LOGFILE_IN for read :$!";
        my $tmp=$_;
         binmode(LOGFILE_IN);   # I need to learn more about binmode
        $tmp =~ s/\0//g;
         while (<LOGFILE_IN>)  
         $tmp =~ s/\015//g;
        $tmp =~ s/\012//g;
        #$tmp =~ s/ //;
        $tmp =~ s/HARP, /J:\//;
         $tmp =~ s/\\/\//g;
        if ($tmp =~ /J:/)
             {
             {
             #print "$tmp";
             my $tmp=$_;        #Strip out characters and make data usable
             if ($tmp =~ m/($podcastdest)\/(.+)/)
            $tmp =~ s/\0//g;
            $tmp =~ s/\015//g;
            $tmp =~ s/\012//g;
             $tmp =~ s/HARP, /$dir\//;
            $tmp =~ s/\\/\//g;
           
            if ($tmp =~ /$dir/) #Find out if the file exists and delete both copies
                 {
                 {
                if ($tmp =~ m/($podcastdest)\/(.+)/)
                    {
                     my $file=$2;
                     my $file=$2;
                      
                     if(-e "$podcastsource/$file")
                    #print "..$podcastsource/$file..\n\n";
                        {
                if(-e "$podcastsource/$file")
                        $file="$podcastsource/$file";
                    {
                        unlink ($file) or warn "Cannot del $file: $!";
                    $file="$podcastsource/$file";
                        print "Deleted $file\n";    #local hard drive                   
                    unlink ($file) or warn "Cannot del $file: $!";;    
                        unlink ($tmp) or warn "Cannot del $tmp: $!";  
                    print "$file\n";
                        print "Deleted $tmp\n";     #Sansa
                        }
                     }
                     }
                 }
                 }
             }
             }
        &deleteplaylist;
         }
         }
       
        #close
        #del
       
     }
     }
sub makedest
sub makedest
     {
     {         #Make directories
        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";
        $directorytmp="$dir$directory1tmp";
        if(!-e $directorytmp)
             {
             {
             $directory1tmp="$directory1tmp\/$directory2make";
             print "Did not find $directorytmp\n";                     
            $directorytmp="j:$directory1tmp";
            mkdir ("$directorytmp") or die "Cannot make $directorytmp :$!";     #incoming logs go here
            #print "$directorytmp\n";
            print "Created $directorytmp\n";     
            if(!-e $directorytmp)
            }
                {
        } # end for each
                print "Did not find $directorytmp\n";                     
                mkdir ("$directorytmp") or die "Cannot make $directorytmp :$!";     #incoming logs go here
                print "Created $directorytmp\n";     
                }
            } # end for each
 
}  
}  


 
sub movefiles
 
     {          
 
sub move
     {
     my $dir = shift;
     my $dir = shift;
   
     opendir DIR, $dir or return;
     opendir DIR, $dir or return;
     my @contents =
     my @contents =
Line 120: Line 160:
         {
         {
         next unless !-l && -d;
         next unless !-l && -d;
         &move($_);
         &movefiles($_);
         $dircontents=$_;
         $dircontents=$_;
        #print "$_\n";
         &createcontents($dircontents);
         &createcontents($dircontents);
         rmdir $_;
         rmdir $_;
Line 131: Line 170:
     {
     {
     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$/ ,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 182:
                 {
                 {
               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 "Copying $newfile\n";
                 print "Copying $newfile\n";
                 copy($filetobecopied, $oldfile) or die "File cannot be copied.";     
                 copy($filetobecopied, $oldfile) or die "File cannot be copied.";     
               
                 }
                 }
             }
             }
Line 160: Line 196:




sub smash  
sub smash # Hulk Smash. Hulk destroy directory (calls destroy files also)
     {
     {
     my $dir = shift;
     my $dir = shift;
     opendir DIR, $dir or return;
     opendir DIR, $dir or return;
Line 175: Line 211:
         &smash($_);
         &smash($_);
         $dircontents=$_;
         $dircontents=$_;
        #print "$_\n";
         &destroycontents($dircontents);
         &destroycontents($dircontents);
         rmdir $_;
         rmdir $_;
      }
        }
     }
     }
      
      
Line 186: Line 221:
     @files = grep(/\.mp3$/,readdir(DIR));
     @files = grep(/\.mp3$/,readdir(DIR));
     closedir(DIR);
     closedir(DIR);
    #print "$dircontents\n";
     foreach $file (@files)  
     foreach $file (@files)  
         {
         {
       
         if ($dircontents =~ m/($podcastdest)\/(.+)/)
         if ($dircontents =~ m/($podcastdest)\/(.+)/)
             {
             {
Line 201: Line 234:
         }
         }
     }
     }
sub deleteplaylist
    {
    if(-e "$LOGFILE_IN")
        {
        if(-w $LOGFILE_IN)
            {
            close LOGFILE_IN or warn "Cannot CLOSE $LOGFILE_IN: $!";;
            unlink $LOGFILE_IN or warn "Cannot del $LOGFILE_IN: $!"; #remove the list
            print "Cleared $LOGFILE_IN\n";
            }
        } 
    }
</pre>
=BAT file=
Here is an old 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.
<pre>
echo off
cls
del "g:\music\podcasts\*" /S /Q
xcopy "D:\music\Ipod\Podcasts\*.mp3" /S "g:\music\Podcasts\"
</pre>
</pre>
=Autoplay Handler=
Under Windows, I added an AutoPlay handler (directions [http://windowsxp.mvps.org/addautoplay.htm here]).  Now when connected, synchronization is possible from the popup window.
[[Image:EventAutoHandler sansa.jpg]]
[[Category:Perl]]
[[Category:Podcasts]]

Latest revision as of 11:01, 7 December 2008

Background

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).

Next step I plan to write something that checks:

  • Cleanup the code
  • Make a playlist (by directory or by group?)
  • 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
  • Implement create directory if not exist routine
  • Read the Sansa On The Go Playlist and delete files that are found
  • Make dest drive setting value
  • REMOVED Check ID3 tag and set if blank - see &johncdvorak


Script Test

Pre-removal of the ANSI Win32 Color references. File:SubDir.jpg

Perl Script

Removed the MP3:: info/tag routines and also the Win32 references. That should make it cross platform.

#!/usr/local/bin/perl
use warnings;
use strict;

use File::Copy;     #http://perldoc.perl.org/File/Copy.html
use Cwd;            #http://perldoc.perl.org/Cwd.html

#################################################################
#######################USER VARIABLES############################
#################################################################

my $podcastsource="C:/Documents and Settings/Mike/My Documents/My Music/Podcasts";

#################################################################

#################################################################
########################System Variables ########################
#################################################################

my $dircontents=();
my $destdir=();
my @newfile=();
my $newfile=();
my @newfiles=();
my @files=();
my $file=();
my $directorytmp=();
my $directory1tmp=();
my $directory2make=();
my @directory2make=();

###########################################################################
# Run the script from the portable and it will determine its current path #
###########################################################################
my $dir = cwd;
$dir =~ s/\///g;
my $LOGFILE_IN="$dir/PLAYLISTS/On The Go.plp";
my $podcastdest="$dir/MUSIC/Podcasts";

#################################################################
######################## Start Processing #######################
#################################################################

print "#################################################################\n";
print "Portable Player Drive is $dir\n";
print "On The Go.plp set to $LOGFILE_IN\n";
print "Music directory is at $podcastdest\n";
print "#################################################################\n\n";
print "Reading On The Go.plp for files to remove from portable:\n";
&OnTheGo;
print "#################################################################\n\n";
print  "Checking source files to move to destination:\n";
&movefiles($podcastsource); #Check what is in the source and not in the dest - copy to dest
print "#################################################################\n\n";
print  "Checking for files removed from source:\n";
&smash($podcastdest);  #Check what is in the dest and not in the source - del from dest
print "#################################################################\n\n";
print "Done\n";

#################################################################
######################### End Processing ########################
#################################################################

#################################################################
######################### Subroutines ########################
#################################################################

sub OnTheGo
    {
    if(-e "$LOGFILE_IN")
        {
        open LOGFILE_IN, $LOGFILE_IN or warn "Cannot open $LOGFILE_IN for read :$!";
        binmode(LOGFILE_IN);    # I need to learn more about binmode
        while (<LOGFILE_IN>) 
            {
            my $tmp=$_;         #Strip out characters and make data usable
            $tmp =~ s/\0//g;
            $tmp =~ s/\015//g;
            $tmp =~ s/\012//g;
            $tmp =~ s/HARP, /$dir\//;
            $tmp =~ s/\\/\//g;
            
            if ($tmp =~ /$dir/) #Find out if the file exists and delete both copies
                {
                if ($tmp =~ m/($podcastdest)\/(.+)/)
                    {
                    my $file=$2;
                    if(-e "$podcastsource/$file")
                        {
                        $file="$podcastsource/$file";
                        unlink ($file) or warn "Cannot del $file: $!";
                        print "Deleted $file\n";    #local hard drive                    
                        unlink ($tmp) or warn "Cannot del $tmp: $!"; 
                        print "Deleted $tmp\n";     #Sansa
                        }
                    }
                }
            }
        &deleteplaylist;
        }
    }
	
sub makedest
    {	        #Make directories
    my $destdirectory=$destdir;
    @directory2make = split(/\//, $destdirectory);
    shift (@directory2make); #drop the first
    $directorytmp="";
    $directory1tmp="";

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

sub movefiles 
    {           
    my $dir = shift;
    
    opendir DIR, $dir or return;
    my @contents =
    map "$dir/$_",
    sort grep !/^\.\.?$/,
    readdir DIR;
    closedir DIR;
    
    foreach (@contents) 
        {
        next unless !-l && -d;
        &movefiles($_);
        $dircontents=$_;
        &createcontents($dircontents);
        rmdir $_;
      }
    }
    
sub createcontents
    {
    opendir(DIR, "$dircontents") or die "Cannot open $dircontents to read: $!";
    @newfiles = grep(/\.mp3$/ ,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 "Copying $newfile\n";
                copy($filetobecopied, $oldfile) or die "File cannot be copied.";    
                }
            }
        }
    }



sub smash # Hulk Smash. Hulk destroy directory (calls destroy files also)
    {  
    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) 
        {
        if ($dircontents =~ m/($podcastdest)\/(.+)/)
            {
            if(!-e "$podcastsource/$2/$file")
                {
                my $del="$podcastdest/$2/$file";
                unlink ($del);      
                print "$del\n";                
                }
            }
        }
    }

sub deleteplaylist
    {
    if(-e "$LOGFILE_IN")
        {
        if(-w $LOGFILE_IN)
            {
            close LOGFILE_IN or warn "Cannot CLOSE $LOGFILE_IN: $!";;
            unlink $LOGFILE_IN or warn "Cannot del $LOGFILE_IN: $!"; #remove the list
            print "Cleared $LOGFILE_IN\n";
            }
        }   
    }

BAT file

Here is an old 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\"

Autoplay Handler

Under Windows, I added an AutoPlay handler (directions here). Now when connected, synchronization is possible from the popup window.

EventAutoHandler sansa.jpg