perl unrar files
I would like perl to unrar files from a folder I specify and after delete the rared files so they dont use HDD space. Files can be in this format: (r(ar|[0-9][0-9])|sfv)
I have unrar installed and I`m new to PERL so please be specific if I need to add something somewhere. Like add this in top of the file and this there.
My script now is like this:
while (1)
{
foreach (`find ${upload_folder}`)
{
chomp;
if ($_ =~ /\.rar$/i)
{
$_=~/^([\W\w]+?)\/([^\/]+)$/;
`rar x "$_" "$1"`;
unlink($_);
}开发者_如何学Go
#...
}
#...
}
Thanks
//Oh yeah and sometimes there can be a folder with multiple parts of rar file called .r01, .r02 .. .r50 and all those parts are actually 1 big rar file split up in many parts
I'm not sure what your question is.
However, if you goal is to actually learn Perl (as opposed to do a quick'n'dirty hack to do a specific function, for which your code is adequate once you add some logic to do rar of multiple files - sorry, I won't bother writing one for you from scratch unless you show what you already tried), there are several ways you can improve your code:
First if all, it's "Perl" (language) and
perl
interpreter. Totally irrelevant to your code but cronologicaly the first comment :)Also,
.sfv
files are usually NOT rar files as far as I know. They contain checksums, not compressed data.All your code should start with:
use strict; use warnings;
This lets Perl tell you about unsafe/dangerous/bad things you may be writing.
You should USUALLY only use `` (system call) when there's no equivalnt internal Perl commmand. In this case:
find
- use Perl nativeFile::Find
modulerar
command - while there may be Perl native rar module (search CPAN to find out), this is one of the rare situations where calling external command may be OK.However, you should ALWAYS specify full path to a system command (security considerations)
unlink()
- like any other IO/filesystem call - should be followed by error checking:unlink($file) or die "Could not delete file $file: $!\n";
精彩评论