I have a folder and inside that I have many subfolders. In those subfolders I have many .html files to be read. I have written the following code to do that. It opens the parent folder and also the first subfolder and it prints only one .html file. It shows error:
NO SUCH FILE OR DIRECTORY
I dont want to change the entire code. Any modifications in the existing code will be good for me.
use FileHandle;
opendir PAR_DIR,"D:\\PERL\\perl_programes\\parent_directory";
while (our $sub_folders = readdir(PAR_DIR))
{
next if(-d $sub_folders);
opendir SUB_DIR,"D:\\PERL\\perl_programes\\parent_directory\\$sub_folders";
while(our $file = readdir(SUB_DIR))
{
next if($file !~ m/\.html/i);
print_file_names($file);
}
close(FUNC_MODEL1);
}
close(FUNC_MODEL);
sub print_file_names()
{
my $fh1 = FileHandle->new("D:\\PERL\\perl_programes\\parent_directory\\$file")
or die "ERROR: $!"; #ERROR HERE
print("$file\n");
}
You’re not extracting the supplied
$fileparameter in theprint_file_names()function.It should be:
Your
-dtest in the outer loop looks wrong too, BTW. You’re sayingnext if -d ...which means that it’ll skip the inner loop for directories, which appears to be the complete opposite of what you require. The only reason it’s working at all is because you’re testing$filewhich is only the filename relative to the path, and not the full path name.Note also:
/as a path separatoropendir($scalar, $path)instead ofopendir(DIR, $path)nb: untested code follows: