题
如何让 Perl 将给定目录的内容读入数组?
反引号 可以做到这一点,但是有没有某种使用“scandir”或类似术语的方法?
解决方案
opendir(D, "/path/to/directory") || die "Can't open directory: $!\n";
while (my $f = readdir(D)) {
print "\$f = $f\n";
}
closedir(D);
编辑:哦,抱歉,错过了“进入数组”部分:
my $d = shift;
opendir(D, "$d") || die "Can't open directory $d: $!\n";
my @list = readdir(D);
closedir(D);
foreach my $f (@list) {
print "\$f = $f\n";
}
编辑2:大多数其他答案都是有效的,但我想发表评论 这个答案 具体来说,其中提供了该解决方案:
opendir(DIR, $somedir) || die "Can't open directory $somedir: $!";
@dots = grep { (!/^\./) && -f "$somedir/$_" } readdir(DIR);
closedir DIR;
首先,记录一下它正在做什么,因为海报没有这样做:它正在传递返回的列表 读目录() 通过一个 grep() 只返回那些文件值(而不是目录、设备、命名管道等)并且不以点开头(这使得列表名称 @dots
误导,但这是由于他从 readdir() 文档复制过来时所做的更改)。由于它限制了它返回的目录的内容,我认为从技术上讲这不是这个问题的正确答案,但它说明了用于过滤文件名的常见习惯用法 珀尔, ,我认为记录下来很有价值。另一个经常看到的例子是:
@list = grep !/^\.\.?$/, readdir(D);
此代码片段读取目录句柄 D 中的所有内容 除了 '.'和'.....',因为很少希望在列表中使用这两个词。
其他提示
一个快速而肮脏的解决方案是使用 全局
@files = glob ('/path/to/dir/*');
IO::目录 很好,并且还提供了绑定哈希接口。
来自 perldoc:
use IO::Dir;
$d = IO::Dir->new(".");
if (defined $d) {
while (defined($_ = $d->read)) { something($_); }
$d->rewind;
while (defined($_ = $d->read)) { something_else($_); }
undef $d;
}
tie %dir, 'IO::Dir', ".";
foreach (keys %dir) {
print $_, " " , $dir{$_}->size,"\n";
}
所以你可以这样做:
tie %dir, 'IO::Dir', $directory_name;
my @dirs = keys %dir;
这将在一行中完成(注意末尾的“*”通配符)
@files = </path/to/directory/*>;
# To demonstrate:
print join(", ", @files);
你可以使用 目录句柄:
use DirHandle;
$d = new DirHandle ".";
if (defined $d)
{
while (defined($_ = $d->read)) { something($_); }
$d->rewind;
while (defined($_ = $d->read)) { something_else($_); }
undef $d;
}
DirHandle
提供了一个替代的、更干净的界面 opendir()
, closedir()
, readdir()
, , 和 rewinddir()
功能。
这是一个递归目录结构并从我编写的备份脚本复制文件的示例。
sub copy_directory {
my ($source, $dest) = @_;
my $start = time;
# get the contents of the directory.
opendir(D, $source);
my @f = readdir(D);
closedir(D);
# recurse through the directory structure and copy files.
foreach my $file (@f) {
# Setup the full path to the source and dest files.
my $filename = $source . "\\" . $file;
my $destfile = $dest . "\\" . $file;
# get the file info for the 2 files.
my $sourceInfo = stat( $filename );
my $destInfo = stat( $destfile );
# make sure the destinatin directory exists.
mkdir( $dest, 0777 );
if ($file eq '.' || $file eq '..') {
} elsif (-d $filename) { # if it's a directory then recurse into it.
#print "entering $filename\n";
copy_directory($filename, $destfile);
} else {
# Only backup the file if it has been created/modified since the last backup
if( (not -e $destfile) || ($sourceInfo->mtime > $destInfo->mtime ) ) {
#print $filename . " -> " . $destfile . "\n";
copy( $filename, $destfile ) or print "Error copying $filename: $!\n";
}
}
}
print "$source copied in " . (time - $start) . " seconds.\n";
}
与上面类似,但我认为最好的版本是(稍作修改)来自“perldoc -f readdir”:
opendir(DIR, $somedir) || die "can't opendir $somedir: $!";
@dots = grep { (!/^\./) && -f "$somedir/$_" } readdir(DIR);
closedir DIR;
从: http://perlmeme.org/faqs/file_io/directory_listing.html
#!/usr/bin/perl
use strict;
use warnings;
my $directory = '/tmp';
opendir (DIR, $directory) or die $!;
while (my $file = readdir(DIR)) {
next if ($file =~ m/^\./);
print "$file\n";
}
以下示例(基于 perldoc -f readdir 中的代码示例)获取打开目录中以句点开头的所有文件(不是目录)。文件名位于数组@dots 中。
#!/usr/bin/perl
use strict;
use warnings;
my $dir = '/tmp';
opendir(DIR, $dir) or die $!;
my @dots
= grep {
/^\./ # Begins with a period
&& -f "$dir/$_" # and is a file
} readdir(DIR);
# Loop through the array printing out the filenames
foreach my $file (@dots) {
print "$file\n";
}
closedir(DIR);
exit 0;
closedir(DIR);
exit 0;