Sunday, January 11, 2015

Perl Script to search a given folder and its sub folders and extract and rar files into the directory they were found in:


#!/usr/bin/perl
use File::Find;
use autodie;
use File::Basename;
use File::Slurp 'read_file';

my $fname = '/home/rarlist.txt';
my $rarfile_text = read_file ($fname); # grep with regular expression

# Search for RAR files

my @content;
find( \&wanted, $ARGV[0]);

open(FILE, ">> $fname");

foreach $filename (@content){
  if ($filename =~ /\.rar/)
    {
    if($rarfile_text !~ $filename) {
      print FILE "$filename\n";
      my($file, $dir, $ext) = fileparse($filename);
      print "Extracting $file to $dir\n";
      chdir $dir;
      `unrar e -u -ai -y $file`;
    }
  }
}

close(FILE);

exit;

sub wanted {
  push @content, $File::Find::name;
  return;
}

No comments: