perl 解压文件
我希望 perl 从我指定的文件夹中解压缩文件,然后删除稀有文件,这样它们就不会使用 HDD 空间。文件可以采用以下格式: (r(ar|[0-9][0-9])|sfv)
我已经安装了 unrar 并且我是 PERL 新手,因此如果我需要在某处添加某些内容,请具体说明。就像将其添加到文件顶部并添加到那里一样。
我的脚本现在是这样的:
while (1)
{
foreach (`find ${upload_folder}`)
{
chomp;
if ($_ =~ /\.rar$/i)
{
$_=~/^([\W\w]+?)\/([^\/]+)$/;
`rar x "$_" "$1"`;
unlink($_);
}
#...
}
#...
}
谢谢
//哦,是的,有时可能会有一个文件夹,其中包含名为 .r01、.r02 .. .r50 的 rar 文件的多个部分,所有这些部分实际上是 1 个大 rar 文件,分为多个部分
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($_);
}
#...
}
#...
}
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你的问题是什么。
然而,如果你的目标是真正学习 Perl(而不是快速完成一个特定的功能,一旦你添加一些逻辑来执行多个文件的 rar 操作,你的代码就足够了 - 抱歉,我赢了不必费心从头开始为您编写代码,除非您展示您已经尝试过的内容),有几种方法可以改进您的代码:
首先,它是“Perl”(语言)和
perl
口译员。与您的代码完全无关,但按时间顺序排列第一个注释:)此外,据我所知,
.sfv
文件通常不是 rar 文件。它们包含校验和,而不是压缩数据。所有代码都应以以下内容开头:
这可以让 Perl 告诉您您可能正在编写的不安全/危险/不好的事情。
当没有等效的内部 Perl 命令时,您通常应该只使用 `` (系统调用)。在这种情况下:
find
- 使用 Perl 原生File::Find
模块rar
命令 - 虽然可能有 Perl 原生 rar 模块(搜索 CPAN 来查找),但这是调用外部命令可能没问题的极少数情况之一。但是,您应该始终指定系统命令的完整路径(安全考虑)
unlink()
- 与任何其他 IO/文件系统调用一样 - 后面应该进行错误检查: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:
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: