Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

unrar from multiple directories

by cesapun (Acolyte)
on Aug 09, 2012 at 18:31 UTC ( #986579=perlquestion: print w/replies, xml ) Need Help??
cesapun has asked for the wisdom of the Perl Monks concerning the following question:

Hello !

I'm trying to write a perl script that search for rar files(and eventually,unrar them into a folder) in a directory that has subdirs.

For example,let's say that i have dir "archive" and this dir contains "cd1","cd2" etc.

Script will get as argument "archive" and will search into it for rar files.Of course,it will open "cd1","cd2" dirs and unrar the content into another folder.

By now,i have a script that search for rar files and unrar only from a dir.But i don't know how to make it to search in every folder.

opendir(DIR, $show_dir); #Opens The Directory my @tempfiles = grep(/\.rar$/,readdir(DIR)); #Scans for files +ending in .rar closedir(DIR); #Closes The Directory my $rar_file = $tempfiles[0]; #Chooses First .rar File Found $rar_file = "$show_dir\/$rar_file"; #Adds Full Path To Rar Fil +e Name my $rar_command = "unrar e \"$rar_file\" \"$output_rar\""; #Cr +eates The Command Used To Unrar print "[INFO] Unraring file, Please Wait.\n"; #Alerts The User + That We Are Going To Unrar `$rar_command`; #Unrars The File

Can someone help me with this?

Many thanks in advance

Replies are listed 'Best First'.
Re: unrar from multiple directories
by 2teez (Vicar) on Aug 09, 2012 at 19:23 UTC

    But i don't know how to make it to search in every folder.
    One of the ways of doing that, is using either a recursive subroutine, or use File::Find module.
    Using a recursive subroutine:
    For Example: To list out all the files in different folders in a directory like so:

    use warnings; use strict; scan_dir( $ARGV[0] ); ## call subroutine sub scan_dir { my ($dir) = @_; if ( -d $dir ) { ## check if directory opendir my $dh, $dir or die "can't open directory: $!"; while ( readdir $dh ) { next if $_ eq '.' or $_ eq '..'; print $_, $/; scan_dir("$dir/$_"); ## recursive call } } }
    This can even get simplier using a File::Find module like so:
    use warnings; use strict; use File::Find qw(find); find(\&scan_dir,$ARGV[0]); sub scan_dir{ return if $_ eq '.' or $_ eq '..'; print $_,$/; }
    NOTE:Please, that the codes above is to show how you can transverse a dircetory. To get your rar files, and "unrar", is left for you to figure out. Am sure you will get around it! :)
    Check perldoc File::Find for more info.

      Thank you very much.

        I will stay all night to find a way to handle with .rar files.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://986579]
Approved by chacham
[vedagiri89]: recently purchased centos server and doing migration of app
[choroba]: did you copy any dependencies?
[choroba]: all XS code needs to be recompiled
[vedagiri89]: install perl5.16 in the new server and try to run appache
[hippo]: Which version of CentOS?
[vedagiri89]: how to fix: Attempt to reload aborted
[hippo]: Ah, presumably 7 then.
[vedagiri89]: CentOS Linux release 7.0.1406 (Core)
[hippo]: How did you install libapreq2?

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2018-06-19 11:27 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (113 votes). Check out past polls.