I am currently writing a program that will eventually compare the files in two directories and display the functions that have changed in each file. However, I have run into a problem when checking to see if what is in the directory is a file or is a sub-directory.
Right now, when I check to see if it is simply a directory with the -d
check, it doesn't catch any of the sub-directories. I have posted parts of my code below.
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";
}
}
As perldoc -f readdir states:
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/$_") {