[英]How can I list files under a directory with a specific name pattern using Perl?
I have a directory /var/spool
and inside that, directories named 我有一个目录
/var/spool
,在其中,目录名为
a b c d e f g h i j k l m n o p q r s t u v x y z
And inside each "letter directory", a directory called " user
" and inside this, many directories called auser1
auser2
auser3
auser4
auser5
... 在每个“字母目录”中,都有一个名为“
user
”的目录,在此目录中,还有许多目录,分别名为auser1
auser2
auser3
auser4
auser5
...
Every user directory contains mail messages and the file names have the following format: 2. 3. 4. 5. etc. 每个用户目录都包含邮件,文件名具有以下格式:2. 3. 4. 5.等等。
How can I list the email files for every user in every directory in the following way: 如何通过以下方式列出每个目录中每个用户的电子邮件文件:
/var/spool/a/user/auser1/11. /var/spool/a/user/auser1/9. /var/spool/a/user/auser1/8. /var/spool/a/user/auser1/10. /var/spool/a/user/auser1/2. /var/spool/a/user/auser1/4. /var/spool/a/user/auser1/12. /var/spool/b/user/buser1/12. /var/spool/b/user/buser1/134. /var/spool/b/user/buser1/144.
etc. 等等
I need that files and then open every single file for modify the header and body. 我需要这些文件,然后打开每个文件来修改标题和正文。 This part I already have, but I need the first part.
我已经有了这部分,但是我需要第一部分。
I am trying this: 我正在尝试:
dir = "/var/spool";
opendir ( DIR, $dir ) || die "No pude abrir el directorio $dirname\n";
while( ($filename = readdir(DIR))){
@directorios1 = `ls -l "$dir/$filename"`;
print("@directorios1\n");
}
closedir(DIR);
But does not work the way I need it. 但是不能按我需要的方式工作。
您可以使用File :: Find 。
使用File :: Find遍历目录树。
As others have noted, use File::Find : 如其他人所述,使用File :: Find :
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
find(\&find_emails => '/var/spool');
sub find_emails {
return unless /\A[0-9]+[.]\z/;
return unless -f $File::Find::name;
process_an_email($File::Find::name);
return;
}
sub process_an_email {
my ($file) = @_;
print "Processing '$file'\n";
}
People keep recommending File::Find, but the other piece that makes it easy is my File::Find::Closures , which provides the convenience functions for you: 人们一直在推荐File :: Find,但是让它变得容易的另一部分是我的File :: Find :: Closures ,它为您提供了便捷的功能:
use File::Find;
use File::Find::Closures qw( find_by_regex );
my( $wanted, $reporter ) = find_by_regex( qr/^\d+\.\z/ );
find( $wanted, @directories_to_search );
my @files = $reporter->();
You don't even need to use File::Find::Closures
. 您甚至不需要使用
File::Find::Closures
。 I wrote the module so that you could lift out the subroutine you wanted and paste it into your own code, perhaps tweaking it to get what you needed. 我编写了该模块,以便您可以提取所需的子例程并将其粘贴到自己的代码中,也许可以对其进行调整以获得所需的内容。
Try this: 尝试这个:
sub browse($);
sub browse($)
{
my $path = $_[0];
#append a / if missing
if($path !~ /\/$/)
{
$path .= '/';
}
#loop through the files contained in the directory
for my $eachFile (glob($path.'*'))
{
#if the file is a directory
if(-d $eachFile)
{
#browse directory recursively
browse($eachFile);
}
else
{
# your file processing here
}
}
}#browse
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.