Я написал процедуру для чтения всех имен и размеров файлов, содержащихся в двух папках, и всех их подпапок.Имена корневых папок указываются в аргументах командной строки, и каждая папка обрабатывается в цикле for, а детали выводятся в отдельный файл для каждой из двух папок.Но я обнаружил, что выводятся только имена файлов / размеры файлов в двух корневых папках, я не могу перейти в подпапки и повторить процесс, поэтому файлы в подкаталогах игнорируются.Трассировки отладки показывают, что команда chdir никогда не выполняется, поэтому что-то не так с моей оценкой, но я не вижу, что это такое.Код выглядит следующим образом:
#!/usr/bin/perl
#!/usr/local/bin/perl
use strict;
use warnings;
my $dir = $ARGV[0];
opendir(DIR, $dir) or die "Could not open directory '$dir' $!";
my @subdirs = readdir(DIR) or die "Unable to read directory '$dir': $!";
for (my $loopcount = 1; $loopcount < 3; $loopcount = $loopcount + 1) {
my $filename = 'FileSize_'.$dir.'.txt';
open (my $fh, '>', $filename) or die "Could not open file '$filename' $!";
for my $subdir (sort @subdirs) {
unless (-d $subdir) {
# Ignore Sub-Directories in this inner loop
# only process files
# print the file name and file size to the output file
print "Processing files\n";
my $size = -s "$dir/$subdir";
print $fh "$subdir"," ","$size\n";
}
elsif (-s "$dir/$subdir") {
# We are here because the entry is a sub-folder and not a file
# if this sub-folder is non-zero size, i.e has files then
# change to this directory and repeat the outer for loop
chdir $subdir;
print "Changing to directory $subdir\n";
print "Processing Files in $subdir\n";
};
}
# We have now processed all the files in First Folder and all it's subdirecorries
# Now assign the second root directory to the $dir variable and repeat the loop
print "Start For Next Directory\n";
$dir = $ARGV[1];
opendir(DIR, $dir) or die "Could not open directory '$dir' $!";
@subdirs = readdir(DIR) or die "Unable to read directory '$dir': $!";;
}
exit 0;
Вызов командной строки: «perl FileComp.pl DiskImage DiskImage1», но выводятся только имена файлов и размеры файлов в корневых папках DiskImage и DiskImage1, все файлыв подпапках игнорируются.Код для перехода в условие «elseif» никогда не выполняется, и код никогда не выполняется, поэтому там есть ошибка.Заранее спасибо за любой совет.