Perl:确定输入是文件还是目录

Kat*_*Kat 1 perl compare file subdirectory

我目前正在编写一个程序,最终将比较两个目录中的文件并显示每个文件中已更改的函数.但是,在检查目录中的内容是文件还是子目录时,我遇到了问题.现在,当我检查它是否只是一个带-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\n\n";
    } elsif((File::Compare::compare("$newDir/$_", "$oldDir/$_") == 1)) {
        print "$_ in new directory $newDirName differs from old directory $oldDirName\n\n";
        print OUTPUTFILE "File: $_ has been update. Please check marked functions for differences\n";
        print OUTPUTFILE "\n\n";
        print OUTPUTFILE "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=\n\n";
    } elsif((File::Compare::compare("$newDir/$_", "$oldDir/$_") < 0)) {
        print "$_ found in new directory $newDirName but not in old directory $oldDirName\n";
        print "Entire file not printed to output file but instead only file name\n";
        print OUTPUTFILE "File: $_ is a new file!\n\n";
        print OUTPUTFILE "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=\n\n";
    }
 }

foreach (@allOldFiles) {
    if((File::Compare::compare("$newDir/$_", "$oldDir/$_") < 0)) {
        print "$_ found in old directory $oldDirName but not in new directory $newDirName\n\n";
    }
 }
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

too*_*lic 6

正如perldoc -f readdir所述:

如果您计划对readdir中的返回值进行filetest测试,那么最好先添加相关目录.否则,因为我们没有那里的chdir,所以它会测试错误的文件.

if(-d "$newDir/$_") {
Run Code Online (Sandbox Code Playgroud)