如何按顺序读取目录中的文件?

发布于 2024-07-11 20:07:15 字数 872 浏览 11 评论 0原文

当我在 Perl 中使用 opendirreaddirlinedir 读取目录时,readdir 函数似乎不存在以任何特定顺序读取文件(我可以告诉)。

我正在读取一个目录,该目录包含由纪元时间戳命名的子目录:

1224161460
1228324260
1229698140

我想按数字顺序读取这些目录,这会将最旧的目录放在前面。

当我使用readdir时,它读取的第一个是1228324260,这是中间的一个。 我知道我可以将目录内容放入数组中并对数组进行排序,但是是否有一个选项可以传递给 readdir 以按排序顺序读取? 或者也许有一种比将所有内容推入数组并对数组进行排序更优雅的方法来实现此目的? 可能也有模块可以做到这一点,但是很难在我们的环境中安装模块,所以除非它是内置模块,否则我宁愿不使用模块......

谢谢!

编辑 根据要求,我发布了我正在使用的代码:

opendir( my $data_dh, $data_dir ) or die "Cannot open $data_dir\n";
while ( my $name = readdir($data_dh) ) {
    next if ( $name eq '.' or $name eq '..' );
    my $full_path = "${data_dir}/${name}";
    next unless ( -d $full_path );
    process_dir($full_path);
}
closedir($data_dh);

When I read a directory in Perl with opendir, readdir, and closedir, the readdir function doesn't seem to read the files in any specific order (that I can tell).

I am reading a directory that has subdirectories named by epoch timestamp:

1224161460
1228324260
1229698140

I want to read in these directories in numerical order, which would put the oldest directories first.

When I use readdir, the first one it reads is 1228324260, which is the middle one. I know I could put the directory contents in an array and sort the array, but is there an option I can pass to readdir to read in sorted order? Or maybe a more elegant way of accomplishing this than pushing everything into array and sorting the array? There are probably modules out there to do this too, but it is difficult to get modules installed in our environment, so unless it is a built-in module I'd prefer to not use modules...

Thanks!

EDIT
As requested, I am posting the code that I am using:

opendir( my $data_dh, $data_dir ) or die "Cannot open $data_dir\n";
while ( my $name = readdir($data_dh) ) {
    next if ( $name eq '.' or $name eq '..' );
    my $full_path = "${data_dir}/${name}";
    next unless ( -d $full_path );
    process_dir($full_path);
}
closedir($data_dh);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

新人笑 2024-07-18 20:07:15

readdir 可以在数组上下文中调用,所以只需这样做:

opendir( my $data_dh, $data_dir) or die "Cannot open $data_dir\n";
my @files = sort { $a <=> $b } readdir($data_dh);
while ( my $name = shift @files ) {
...

readdir can be called in array context, so just do this:

opendir( my $data_dh, $data_dir) or die "Cannot open $data_dir\n";
my @files = sort { $a <=> $b } readdir($data_dh);
while ( my $name = shift @files ) {
...
疯到世界奔溃 2024-07-18 20:07:15

你可以用一点 Glob 魔法来尝试一下,glob 似乎以排序的方式运行,所以这个:

#  Glob in scalar context iterates the result set internally
while( defined( my $dir = glob($dir . '/*' ) ) ){ 
     print $dir, "\n";
    # $dir is fed ordered and with full names. 
}

or

# Glob in list context returns all results. 
for( glob($dir.'/*' ) ){ 
  print $dir , "\n";
  # also ordered. 
}

应该可以工作。 请小心 glob,因为这:

 for(0..20){ 
   printf "%30s|%30s\n", glob($dir.'/*' ), glob($dir.'/*' );
 }

做了一些半神奇的事情,并在每行上打印两次 dir 内容。 IE:

/foo/bar/a  |  /foo/bar/a
/foo/bar/b  |  /foo/bar/b
/foo/bar/c  |  /foo/bar/c
/foo/bar/d  |  /foo/bar/d

You can try it with a bit of Glob magic, glob appears to function in a sorted manner, so this:

#  Glob in scalar context iterates the result set internally
while( defined( my $dir = glob($dir . '/*' ) ) ){ 
     print $dir, "\n";
    # $dir is fed ordered and with full names. 
}

or

# Glob in list context returns all results. 
for( glob($dir.'/*' ) ){ 
  print $dir , "\n";
  # also ordered. 
}

should work. Just be careful with glob, because this:

 for(0..20){ 
   printf "%30s|%30s\n", glob($dir.'/*' ), glob($dir.'/*' );
 }

does something semi-magical, and prints the dir contents twice on each line. ie:

/foo/bar/a  |  /foo/bar/a
/foo/bar/b  |  /foo/bar/b
/foo/bar/c  |  /foo/bar/c
/foo/bar/d  |  /foo/bar/d
岁月无声 2024-07-18 20:07:15

只需在要重新排序的任何列表运算符前面添加一个 sort 即可。 您也不需要将结果存储在数组中。 您可以使用foreach

opendir my($dh), $dirname or die "Could not open directory [$dirname]: $!";

foreach my $file ( sort { $a <=> $b } readdir $dh )
    {
    ...
    }

Just throw a sort in front of any list operator that you want to re-order. You don't need to store the results in an array either. You can use a foreach:

opendir my($dh), $dirname or die "Could not open directory [$dirname]: $!";

foreach my $file ( sort { $a <=> $b } readdir $dh )
    {
    ...
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文