Actions

Podcast Synch Batch File: Difference between revisions

From Mike Beane's Blog

(New page: 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 i...)
 
No edit summary
Line 12: Line 12:


Next step I plan to write something that checks:
Next step I plan to write something that checks:
* Does file exist in both places - don't copy
* <s>Does file exist in both places - don't copy</s>
* Does file exist in dest and not in source - del dest
* <s>Does file exist in dest and not in source - del dest</s>
* Does file exist in source and not in dest - copy source
* <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
* Do I have enough room to copy source -> dest - copy oldest files first
* Implement create directory if not exist routine
=Perl Script=
<pre>
use File::Copy;
my $podcastsource="D:/music/Ipod/Podcasts";
my $podcastdest="J:/MUSIC/Podcasts";
my $dircontents=();
print  "Checking files to move over.\n";
&move($podcastsource); #Check what is in the source and not in the dest - copy to dest
print  "Checking files to remove.\n";
&smash($podcastdest);  #Check what is in the dest and not in the source - del from dest
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=$_;
        #print "$_\n";
        &createcontents($dircontents);
        rmdir $_;
      }
    }
   
sub createcontents
    {
    opendir(DIR, "$dircontents") or die "Cannot open $dircontents to read: $!";
    @newfiles = grep(/\.mp3$/,readdir(DIR));
    closedir(DIR);
    #print "$dircontents\n";
    foreach $newfile (@newfiles)
        {
       
        if ($dircontents =~ m/($podcastsource)\/(.+)/)
            {
            my $testdir="$podcastdest/$2/$newfile";
           
            if(!-e $testdir)
                {
               
                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
    {
    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=$_;
        #print "$_\n";
        &destroycontents($dircontents);
        rmdir $_;
      }
    }
   
sub destroycontents
    {
    opendir(DIR, "$dircontents") or die "Cannot open $dircontents to read: $!";
    @files = grep(/\.mp3$/,readdir(DIR));
    closedir(DIR);
    #print "$dircontents\n";
    foreach $file (@files)
        {
       
        if ($dircontents =~ m/($podcastdest)\/(.+)/)
            {
            if(!-e "$podcastsource/$2/$file")
                {
                my $del="$podcastdest/$2/$file";
                unlink ($del);     
                print "$del\n";               
                }
            }
        }
    }
</pre>

Revision as of 03:00, 24 May 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

Perl Script

use File::Copy;

my $podcastsource="D:/music/Ipod/Podcasts";
my $podcastdest="J:/MUSIC/Podcasts";
my $dircontents=();

print  "Checking files to move over.\n";
&move($podcastsource); #Check what is in the source and not in the dest - copy to dest
print  "Checking files to remove.\n";
&smash($podcastdest);  #Check what is in the dest and not in the source - del from dest


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=$_;
        #print "$_\n";
        &createcontents($dircontents);
        rmdir $_;
      }
    }
    
sub createcontents
    {
    opendir(DIR, "$dircontents") or die "Cannot open $dircontents to read: $!";
    @newfiles = grep(/\.mp3$/,readdir(DIR));
    closedir(DIR);
    #print "$dircontents\n";
    foreach $newfile (@newfiles) 
        {
        
        if ($dircontents =~ m/($podcastsource)\/(.+)/)
            {
            my $testdir="$podcastdest/$2/$newfile";
            
            if(!-e $testdir)
                {
                
                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 
    {
    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=$_;
        #print "$_\n";
        &destroycontents($dircontents);
        rmdir $_;
      }
    }
    
sub destroycontents
    {
    opendir(DIR, "$dircontents") or die "Cannot open $dircontents to read: $!";
    @files = grep(/\.mp3$/,readdir(DIR));
    closedir(DIR);
    #print "$dircontents\n";
    foreach $file (@files) 
        {
        
        if ($dircontents =~ m/($podcastdest)\/(.+)/)
            {
            if(!-e "$podcastsource/$2/$file")
                {
                my $del="$podcastdest/$2/$file";
                unlink ($del);      
                print "$del\n";                
                }
            }
        }
    }