Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 220755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:58:08+00:00 2026-05-11T18:58:08+00:00

I have a folder and inside that I have many subfolders. In those subfolders

  • 0

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");
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:58:09+00:00Added an answer on May 11, 2026 at 6:58 pm

    You’re not extracting the supplied $file parameter in the print_file_names() function.

    It should be:

    sub print_file_names()
    {
        my $file = shift;
        ...
    }
    

    Your -d test in the outer loop looks wrong too, BTW. You’re saying next 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 $file which is only the filename relative to the path, and not the full path name.

    Note also:

    1. Perl on Windows copes fine with / as a path separator
    2. Set your parent directory once, and then derive other paths from that
    3. Use opendir($scalar, $path) instead of opendir(DIR, $path)

    nb: untested code follows:

    use strict;
    use warnings;
    use FileHandle;
    
    my $parent = "D:/PERL/perl_programes/parent_directory";
    
    my ($par_dir, $sub_dir);
    opendir($par_dir, $parent);
    while (my $sub_folders = readdir($par_dir)) {
        next if ($sub_folders =~ /^..?$/);  # skip . and ..
        my $path = $parent . '/' . $sub_folders;
        next unless (-d $path);   # skip anything that isn't a directory
    
        opendir($sub_dir, $path);
        while (my $file = readdir($sub_dir)) {
            next unless $file =~ /\.html?$/i;
            my $full_path = $path . '/' . $file;
            print_file_names($full_path);    
        }
        closedir($sub_dir);
    }
    closedir($par_dir);
    
    sub print_file_names()
    {
         my $file = shift;
         my $fh1 = FileHandle->new($file) 
               or die "ERROR: $!"; #ERROR HERE 
         print("$file\n");
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have HTML pages that I put inside temp folder(Outside WEB-INF directory). I have
So I have this code inside my lib/ folder: class GlobalConfig::SetHelper def self.yes_no_input(configuration) value
imagine i have made a co. Then I remove a folder inside the code
I have a folder which file name is: List of names. Inside that folder
I have, let's say, www.website.org/folder/ which inside has the following .htaccess RewriteEngine On RewriteRule
On my website I have an affiliates/ folder. Inside that I have showgames.php I
In my project, have setup an admin panel under a folder inside controllers folder
I have one created one folder called MYFOLDER inside my folder in have one
I have a php page inside a folder on my website. I need to
Every now and then I have a problem with SVN inside eclipse folder gets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.