LinHES Forums
http://forum.linhes.org/

Script to delete orphaned .png files in /myth/tv
http://forum.linhes.org/viewtopic.php?f=24&t=22455
Page 1 of 1

Author:  bruce_s01 [ Fri Feb 24, 2012 8:42 am ]
Post subject:  Script to delete orphaned .png files in /myth/tv

On finishing watching a file there appears to be a bit of activity from the system, if you delete the file before this stops, an orphaned ..mpg.png file is left. These files are not big, but they could build up over time so I wrote a short script to delete them:
Code:
#!/usr/bin/perl -s

if (defined ($ARGV[0])) {
   $CMDDIR = $ARGV[0] ;
}
else {
   $CMDDIR = "." ;
}

print "DIR is:".$CMDDIR."\n";

if ( -d $CMDDIR ) {
   print "And it is a Directory \n";
   chdir ($CMDDIR) ;

   opendir(DIR, ".") or die "Can't open current directory $! \n";
   
   print "Testing 1\n";

#find .mpg...png files in directory
   @pngnames = grep { /\.mpg.*png$/ } readdir(DIR) or die "No Target Files present:$!\n";
   closedir(DIR);

   foreach $pngname (@pngnames) {
      next if $pngname =~ /^\.\.?$/ ; # skip .. and .

#Shouldn't be any directory entries
      if (-d $name) {
         next;   #can skip to the next name in the loop
      }

# create name of mpg from the png
      $mpgname = $pngname;
      $mpgname =~ s/\..*png$// ;
      $mpgname = $mpgname.".mpg" ;

#If the mpg does not exist, delete the png file
      if (!( -e $mpgname)) {
         print $pngname." Is an Orphan from ".$mpgname." \n";
         print "Deleting... \n";
         unlink ($pngname) ;

      }
   }
}



Just pass the directory path to the tv directory in the command line.
It's not that intelligent, if no pathname is passed, it just uses the current directory, it doesn't do anything clever like looking up the db.

Regards
Bruce S.

Author:  slowtolearn [ Fri Feb 24, 2012 4:30 pm ]
Post subject:  Re: Script to delete orphaned .png files in /myth/tv

bruce_s01 wrote:
On finishing watching a file there appears to be a bit of activity from the system, if you delete the file before this stops, an orphaned ..mpg.png file is left. These files are not big, but they could build up over time so I wrote a short script to delete them:
Code:
#!/usr/bin/perl -s

if (defined ($ARGV[0])) {
   $CMDDIR = $ARGV[0] ;
}
else {
   $CMDDIR = "." ;
}

print "DIR is:".$CMDDIR."\n";

if ( -d $CMDDIR ) {
   print "And it is a Directory \n";
   chdir ($CMDDIR) ;

   opendir(DIR, ".") or die "Can't open current directory $! \n";
   
   print "Testing 1\n";

#find .mpg...png files in directory
   @pngnames = grep { /\.mpg.*png$/ } readdir(DIR) or die "No Target Files present:$!\n";
   closedir(DIR);

   foreach $pngname (@pngnames) {
      next if $pngname =~ /^\.\.?$/ ; # skip .. and .

#Shouldn't be any directory entries
      if (-d $name) {
         next;   #can skip to the next name in the loop
      }

# create name of mpg from the png
      $mpgname = $pngname;
      $mpgname =~ s/\..*png$// ;
      $mpgname = $mpgname.".mpg" ;

#If the mpg does not exist, delete the png file
      if (!( -e $mpgname)) {
         print $pngname." Is an Orphan from ".$mpgname." \n";
         print "Deleting... \n";
         unlink ($pngname) ;

      }
   }
}



Just pass the directory path to the tv directory in the command line.
It's not that intelligent, if no pathname is passed, it just uses the current directory, it doesn't do anything clever like looking up the db.

Regards
Bruce S.

Nice tip! A bash alternative (not trying to hijack, this is one of the things I love about Linux - "How can I skin this cat"!):
Code:
#!/bin/bash

for file in *.mpg.png
do
   [ ! -f "${file%.*}" ] && {
      rm -f $file
   }
done


WARNING: Unlike Bruce's script this one has no intelligence, it will blindly remove any .mpg.png files that have no corresponding .mpg file in your current working directory.

Author:  tjc [ Fri Feb 24, 2012 8:06 pm ]
Post subject:  Re: Script to delete orphaned .png files in /myth/tv

I remember a couple old scripts for doing this too. This old thread includes some examples, and someone (maybe brfransen?) wrote a fancy combined one that found files with no db entry and db entries with no file, that defaulted to safe "report only" mode and could be told to clean up... http://forum.linhes.org/viewtopic.php?f=6&t=6150

Author:  mattbatt [ Fri Feb 24, 2012 9:05 pm ]
Post subject:  Re: Script to delete orphaned .png files in /myth/tv

If you remove a png file won't myth just make a new one when you scroll over it or play it?

Author:  tjc [ Sat Feb 25, 2012 11:12 am ]
Post subject:  Re: Script to delete orphaned .png files in /myth/tv

Yes, but I think the point is to delete .png files that no longer have a corresponding .mpg file or DB record.

OBTW - The script I was trying to remember last night was myth.find_orphans.pl which seems to have fallen by the wayside. http://www.mythtv.org/wiki/Myth.find_orphans.pl The modern replacement is this script: http://www.mythtv.org/wiki/Find_orphans.py

Author:  jams [ Sat Feb 25, 2012 6:02 pm ]
Post subject:  Re: Script to delete orphaned .png files in /myth/tv

you will find that a modified versino of find_orphans.py is already included.

Author:  tjc [ Sun Feb 26, 2012 11:55 am ]
Post subject:  Re: Script to delete orphaned .png files in /myth/tv

Seems to be missing in 6.04, but that may just mean that I don't have some optional package installed.

Author:  jams [ Sun Feb 26, 2012 12:16 pm ]
Post subject:  Re: Script to delete orphaned .png files in /myth/tv

7.2+ only

Author:  bruce_s01 [ Mon Jun 18, 2012 5:50 am ]
Post subject:  Re: Script to delete orphaned .png files in /myth/tv

Perhaps I should have looked a bit more closely. :)

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/