Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Perl - Read folders recursively

Posted on 2011-05-10
Medium Priority
Last Modified: 2012-05-11
I would like to build code that runs on the file system (from a specific folders onwards) and retrieve all files that do not have extension of *.atr. What is the best method to do that? Should i use glob ?
Thank you
Question by:tamirmilo
LVL 27

Accepted Solution

wilcoxon earned 1000 total points
ID: 35729848
This should do what you want...

use strict;
use warnings;

my $root = shift or die "Usage: $0 root_folder\n";
my $rx = shift || '\.atr$';

my @no_match = process($root);

print "files that did not match $rx:\n", join("\n", @no_match), "\n";

sub process {
    my ($dir) = @_;
    my (@dirs, @bad);
    opendir DIR, $dir or die "could not open dir $dir: $!";
    while (my $fil = readdir DIR) {
        if (-d "$dir/$fil" and $fil !~ m{^\.+$}) {
            push @dirs, "$dir/$fil";
        } elsif (-f "$dir/$fil" and $fil !~ m{$rx}) {
            push @bad, "$dir/$fil";
    closedir DIR;
    foreach my $d (@dirs) {
        push @bad, process($d);
    return @bad;

Open in new window

LVL 13

Assisted Solution

by:Carl Bohman
Carl Bohman earned 1000 total points
ID: 35730266
It looks like wilcoxon's solution should work.  In general, though, maintenance is easier if you leverage standard modules (such as File::Find) when it makes sense.  I'll use wilcoxon's same command line interface for consistency.


use strict;
use warnings;

my $root = shift or die "Usage: $0 root_folder\n";
my $rx = shift || '\.atr$';

# This pre-compiles the regex so that it doesn't need to be compiled each time it is used.
my $crx = qr/$rx/ or die "Unable to compile regex: $rx\n";

my @no_match;

sub wanted
   push @no_match, $File::Find::name if (-f _ && ! /$regex/)

find(\&wanted, $root);

print "files that did not match $rx:\n", join("\n", @no_match), "\n";

Open in new window


Author Closing Comment

ID: 35736181
Thank you both.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question