FlipsideReality Once upon a time, in a land far far away…

11Sep/083

Recursive unrar unzip perl script

It's a bit hacky, but it works.

#!/usr/bin/perl
use File::Find;
use File::Spec;

if (@ARGV[0] eq '')
{
	print "useage: $0 foldername\n";
}
else
{
	print "Exctacting for folder ". $ARGV[0] ."\n";
	find ( \&extract_dir , @ARGV[0]);
	print (++$n,": $_\n") foreach (@elist) ;
}

exit;

sub extract_dir
{
next if -d $_;
next if /^\./;
my $ff = $File::Find::name;

	# deal with RAR files
	unrar($_,$ff);
	#deal with ZIP files
	if ($ff =~ /\.zip$/i )
	{
		print "unzip $ff";
		system ('unzip','-qq', "$_");
		if ($? != 0)
		{
			push (@elist, $ff);
		}
		else
		{
			print "\nrm -f $ff\n";
			system ('rm', '-f', $_);
		}
	}
}

sub unrar
# takes filename, full filename
{
	if ($_[0] =~ /\.rar$/i)
	{
		if ($_[0] =~ /\.part[0-9]+?\.rar$/i)
		{
			if ($_[0] =~ /\.part[0]+?1\.rar$/i)
			{
				print "unrar x $_[1] \n";
				system ('unrar','x','-y','-inul', "$_[0]");
				if ($? != 0)
				{
					push (@elist, "$_[1]");
				}
				else
				{
					my $todel = $_[0];
					$todel =~ s/\.part[0]+?1\.rar$/\.part*\.rar/g ;
					print "find ./ -name \"$todel\" -exec rm {} \\\;\n";
					system ("find ./ -name \"$todel\" -exec rm {} \\\;");
				}
			}
		}
		else
		{
			print "unrar x $_[1] \n";
			system ('unrar','x','-y','-inul', "$_[0]");
			if ($? != 0)
			{
				push (@elist, "$_[1]");
			}
			else
			{
				my $todel = $_[0];
				$todel =~ s/\.rar$/\.r*/g ;
				print "find ./ -name \"$todel\" -exec rm {} \\\;\n";
				system ("find ./ -name \"$todel\" -exec rm {} \\\;");
			}
		}
	}
}
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Comments (3) Trackbacks (0)
  1. love this script been using it ever since it was posted thanks a lot :)

  2. this is perfect!
    thanks.


Leave a comment


No trackbacks yet.