All pastes #472971 Raw Edit

Untitled

public text v1 · immutable
·published 2007-05-05 23:28 UTC
rendered paste body
#!/usr/bin/perl

use strict;
use File::Find;
use Time::HiRes;
use Data::Dumper;

my %hash;
my @remove_files;

opendir DIR, "./" or die "Bad dir : $!\n";
my @directories = readdir(*DIR);
my @fixed_dirs;
map { push ( @fixed_dirs, $_ ) if ( $_ !~ /^\.+$/) && -d $_ } @directories;
print "FIXED:  ",join("\n",@fixed_dirs),"\n";


#[03:30PM]<mauke> eval: @x = qw(a.rar b.no c.rar d.r00 e.otherwise a.r01 d.rar x y z); [ sort { reverse($a) cmp reverse($b) } @x ]
#[03:30PM]<buubot> mauke:  ['d.r00','a.r01','e.otherwise','b.no','a.rar','c.rar','d.rar','x','y','z']
# same idea ...
#[03:34PM]<theorem> I see how it works, but not why it works.
#[03:34PM]<mauke> it sorts strings by lexical comparison, only backwards
#[03:35PM]<mauke> so files with the same extension will be grouped together
#[03:36PM]<mauke> eval: @x = qw(a.rar b.no c.rar d.r00 e.otherwise a.r01 d.rar x y z); [ sort { $a cmp $b } map scalar reverse($_), @x ]
#[03:36PM]<buubot> mauke:  ['00r.d','10r.a','esiwrehto.e','on.b','rar.a','rar.c','rar.d','x','y','z']
#$b =~ /\.rar\z/ - $a =~ /\.rar\z/) || $a cmp $b

my %options = ( preprocess => sub { reverse sort { reverse($a) cmp reverse($b) } @_ },
                wanted  => \&wanted
                );
find( \%options , @fixed_dirs);
#find(\&not_wanted, @fixed_dirs) ;
#print "Remove these files? : ",join("\n", @remove_files ),"\n";
close DIR;
print "Next\n";
print Dumper(%hash);

sub wanted {
        if ( /(\.part\d\d)*\.([ra\d]{3})$/ ) {
                if ( ! $hash{$File::Find::dir}{'done'} ) {
                        my $origfile = $_;
                        $_ =~ s/(\.part\d\d)*\.([ra\d]{3})|(\d{3})$//;
                        my $shortname = $_;
                        if ( ! map { $shortname eq $_ } @{$hash{$File::Find::dir}{'done'}} ) {
                                push ( @{$hash{$File::Find::dir}{'extracted'}}, $origfile);
#                               system("unrar e -o- $origfile ");
                                print "unrar e -o- $origfile\n";
                                push ( @{$hash{$File::Find::dir}{'done'}}, $shortname);
#                               rename ( "$File::Find::dir","$File::Find::dir.EXTRACTED");
                        }
                }
        }
}

sub not_wanted {
        if ( /(\.r\d{2}$)|(\.\d{3}$)|(\.rar$)/ ) {
                sleep 0.5;
                push( @remove_files, $_);
        }
}