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 8186133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:05:23+00:00 2026-06-07T02:05:23+00:00

So for the program I am writing, what I would like for it to

  • 0

So for the program I am writing, what I would like for it to do is search through all of the subdirectories within a directory. If the subdirectory name contains a word, let’s say “foo”, then the program will open this subdirectory and perform a function on the files within the subdirectory. Can anybody give me some help on how to go about this? it also needs to be recursive. Thanks in advance

  • 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-06-07T02:05:25+00:00Added an answer on June 7, 2026 at 2:05 am

    This can be done using the File::Find module, but I believe Path::Class is superior even though it isn’t a core module and will likely need installing.

    This program finds the files wanted and calls process to process them. At present the process subroutine simply prints the name of the file for testing.

    use strict;
    use warnings;
    
    use Path::Class;
    
    my $dir = dir '/path/to/root/directory';
    
    $dir->recurse(callback => sub {
      my $node = shift;
      return if $node->is_dir;
      my $parent = $node->parent;
      if ($parent->basename =~ /foo/) {
        process($node);
      }
    });
    
    sub process {
      my $file = shift;
      print $file, "\n";
    }
    

    Update

    If you prefer, this program performs the same task using File::Find.

    use strict;
    use warnings;
    
    use File::Find;
    use File::Basename qw/ basename /;
    
    my $dir = '/path/to/root/directory';
    
    find(sub {
      return unless -f;
      if (basename($File::Find::dir) =~ /foo/) {
        process($File::Find::name);
      }
    }, $dir);
    
    sub process {
      my $file = shift;
      print $file, "\n";
    }
    

    Update

    As requested, here is a further solution using Path::Class::Rule for comparison. As daxim suggested the code is a little shorter.

    use strict;
    use warnings;
    
    use Path::Class::Rule;
    
    my $rule = Path::Class::Rule->new;
    $rule->file->and(sub { $_->parent->basename =~ /foo/ });
    
    my $next = $rule->iter('/path/to/root/directory');
    while ( my $file = $next->() ) {
      process($file);
    }
    
    sub process {
      my $file = shift;
      print $file, "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the python program I am writing I would like to give the opportunity
I'm currently writing a program in Python, and I would like to determine the
I'm writing a blackjack program and would like to be able to write the
I'm writing a physics simulating program, and found after() useful. I once would like
I'm currently writing a program in which I would like to access the variable
I am writing a program for WP7 in C# where I would like to
I am currently trying to make a Writing program. I would like the functionality
I am writing a program in C#.NET I would like to gather the total
I'm currently writing a program in Java that I would like to use to
I would like to distribute a program I'm halfway done writing but I'm having

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.