打印给定目录的文件和子目录 [英] Print files and subdirectories of given directory

查看:130
本文介绍了打印给定目录的文件和子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从给定目录中获取所有文件和目录,但是我无法指定类型(文件/目录).什么都没有打印.我在做什么错以及如何解决.这是代码:

I am trying to get all files and directories from a given directory but I can't specify what is the type (file/ directory). Nothing is being printed. What I am doing wrong and how to solve it. Here is the code:

sub DoSearch {
    my $currNode = shift;
    my $currentDir = opendir (my $dirHandler, $currNode->rootDirectory) or die $!;

    while (my $node = readdir($dirHandler)) {
        if ($node eq '.' or $node eq '..') {
            next;
        }

        print "File: " . $node . "\n" if -f $node;
        print "Directory " . $node . "\n" if -d $node;
   }

   closedir($dirHandler);
}

推荐答案

readdir仅返回节点名称,而不包含任何路径信息.如果未指定路径,文件测试操作员将在当前工作目录中查找,并且由于当前目录不是$currNode->rootDirectory,因此找不到它们

readdir returns only the node name without any path information. The file test operators will look in the current working directory if no path is specified, and because the current directory isn't $currNode->rootDirectory they won't be found

我建议您使用 File::Spec::Functions 核心模块中的rel2abs将节点名称与路径结合起来.您可以使用字符串连接,但是库函数可以处理一些极端情况,例如目录是否以斜杠结尾

I suggest you use rel2abs from the File::Spec::Functions core module to combine the node name with the path. You can use string concatenation, but the library function takes care of corner cases like whether the directory ends with a slash

还值得指出的是,Perl标识符最常出现在snake_case中,熟悉该语言的人将感谢您不使用大写字母.对于标识符的第一个字符,尤其应避免使用它们,因为类似的名称将保留给全局名称(如包名称)

It's also worth pointing out that Perl identifiers are most often in snake_case, and people familiar with the language would thank you for not using capital letters. They should especially be avoided for the first character of an identifier, as names like that are reserved for globals like package names

我认为您的子程序应如下所示

I think your subroutine should look like this

use File::Spec::Functions 'rel2abs';

sub do_search {
    my ($curr_node) = @_;
    my $dir         = $curr_node->rootDirectory;

    opendir my $dh, $dir or die qq{Unable to open directory "$dir": $!};

    while ( my $node = readdir $dh ) {
        next if $node eq '.' or $node eq '..';

        my $fullname = rel2abs($node, $dir);

        print "File:     $node\n" if -f $fullname;
        print "Directory $node\n" if -d $fullname;
   }
}

另一种方法是将当前工作目录设置为正在读取的目录.这样就无需操纵文件路径,但是您需要在更改之前和之后保存并还原原始工作目录

An alternative method is to set the current working directory to the directory being read. That way there is no need to manipulate file paths, but you would need to save and restore the original working directory before and after changing it

Cwd 核心模块提供getcwd,您的代码将如下所示

The Cwd core module provides getcwd and your code would look like this

use Cwd 'getcwd';

sub do_search {
    my ($curr_node) = @_;

    my $cwd = getcwd;
    chdir $curr_node->rootDirectory or die $!;

    opendir my $dh, '.' or die $!;

    while ( my $node = readdir $dh ) {
        next if $node eq '.' or $node eq '..';

        print "File: \n" if -f $node;
        print "Directory $node\n" if -d $node;
   }

   chdir $cwd or die $!;
}

这篇关于打印给定目录的文件和子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆