English 中文(简体)
确定输入是文件还是目录
原标题:Determining whether an input is a file or directory

我目前正在写入一个程序, 该程序将最终比较两个目录中的文件, 并显示每个文件中已更改的函数。 然而, 当检查目录中的内容是文件还是子目录时, 我遇到了一个问题 。

现在,当我检查它是否只是一个带有 < code> - d 检查的目录时, 它没有捕获任何子目录。 我在下面张贴了部分代码 。

opendir newDir, $newDir;
my @allNewFiles = grep { $_ ne  .  and $_ ne  .. } readdir newDir;
closedir newDir;

opendir oldDir, $oldDir;
my @allOldFiles = grep { $_ ne  .  and $_ ne  .. } readdir oldDir;
closedir oldDir;


foreach (@allNewFiles) {
    if(-d $_) {
        print "$_ is not a file and therefore is not able to be compared

";
    } elsif((File::Compare::compare("$newDir/$_", "$oldDir/$_") == 1)) {
        print "$_ in new directory $newDirName differs from old directory $oldDirName

";
        print OUTPUTFILE "File: $_ has been update. Please check marked functions for differences
";
        print OUTPUTFILE "

";
        print OUTPUTFILE "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

";
    } elsif((File::Compare::compare("$newDir/$_", "$oldDir/$_") < 0)) {
        print "$_ found in new directory $newDirName but not in old directory $oldDirName
";
        print "Entire file not printed to output file but instead only file name
";
        print OUTPUTFILE "File: $_ is a new file!

";
        print OUTPUTFILE "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

";
    }
 }

foreach (@allOldFiles) {
    if((File::Compare::compare("$newDir/$_", "$oldDir/$_") < 0)) {
        print "$_ found in old directory $oldDirName but not in new directory $newDirName

";
    }
 }
最佳回答

http://perldoc.perl.org/works/readdir.html>perldoc -f readdir 指出:

If you re planning to filetest the return values out of a readdir, you d better prepend the directory in question. Otherwise, because we didn t chdir there, it would have been testing the wrong file.

if(-d "$newDir/$_") {
问题回答

http://search.cpan.org/~kwilliams/Path-Class-0.25/"rel="nofollow">Path::Class

use strict;
use warnings;
use Path::Class;


my @allNewFiles = grep { !$_->is_dir } dir("/newDir")->children;

使用循环呼叫先获取文件列表,然后对文件操作 。

my $filesA = {};
my $filesB = {};

# you are passing in a ref to filesA or B so no return is needed.
sub getFiles {
  my ($dir, $fileList) = @_;

  foreach my $file (glob("*")) {
    if(-d $file) {
      getFiles($dir . "/" . $file, $fileList); # full relative path saved
    } else {
      $fileList{$dir . "/" . $file}++;         # only files are put into list
    }
  }
}

# get the files list
my $filesA = getFiles($dirA);
my $filesB = getFiles($dirB);

# check them by using the keys from the 2 lists created.




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签