Christy
Christy

Reputation:

How do I check for a sub-subdirectory in Perl?

I have a folder called Client which contains many subfolders. I want to create a Perl script to look at each of those subfolders and check for a folder there. If it is there, I want to skip it and move on, if it is not there, I want to create it and do some processing.

How do I go about looping through all of the subfolders and checking for the directory I want? I have found a lot of information on how to get all the files in a folder and/or subfolders, but nothing on checking for a directory within each subfolder.

Upvotes: 2

Views: 5498

Answers (4)

Andy Ross
Andy Ross

Reputation: 12043

Augh! Too much complexity in the other answers. The original question doesn't appear to be asking for a recursive traversal. As far as I can see, this is a perfectly sensible solution, and vastly more readable to boot:

foreach my $dir (glob "Client/*") {
    next if ! -d $dir;              # skip if it's not a directory
    next if -d "$dir/subfolder";    # skip if subfolder already exists
    mkdir "$dir/subfolder" or die;  # create it
    do_some_processing();           # do some processing
}

Seriously folks: opendir/readdir? Really?

Upvotes: 12

brian d foy
brian d foy

Reputation: 132792

It's pretty easy once you break it into steps. Get a list of the subdirectories with glob then see which ones don't have the second-level directory. If you are using a File::Find-like module, you are probably doing too much work:

#!perl

use strict;
use warnings;

use File::Spec::Functions;

my $start  = 'Clients';
my $subdir = 'already_there';

# @queue is the list of directories you need to process
my @queue  = grep { ! -d catfile( $_, $subdir ) }   # filter for the second level
             grep { -d }                            # filter for directories
             glob catfile( $start, '*' );           # everything below $start   

Upvotes: 4

chaos
chaos

Reputation: 124297

#!/usr/bin/perl

use strict;

use Fcntl qw( :DEFAULT :flock :seek );
use File::Spec;
use IO::Handle;

my $startdir = shift @ARGV || '.';
die "$startdir is not a directory\n"
    unless -d $startdir;
my $verify_dir_name = 'MyDir';

my $dh = new IO::Handle;
opendir $dh, $startdir or
    die "Cannot open $startdir: $!\n";
while(defined(my $cont = readdir($dh))) {
    next
        if $cont eq '.' || $cont eq '..';
    my $fullpath = File::Spec->catfile($dir, $cont);
    next
        unless -d $fullpath && -r $fullpath && -w $fullpath;
    my $verify_path = File::Spec->catfile($fullpath, $verify_dir_name);
    next
        if -d $verify_path;
    mkdir($verify_path, 0755);
    # do whatever other operations you want to $verify_path
}
closedir($dh);

Upvotes: 2

David Harris
David Harris

Reputation: 2340

The short answer is use File::FInd.

The long answer is first write a subroutine that validates the existence of the folder and if the folder is not there, create it and then do the processing needed. Then invoke the find method of the File::Find module with a reference to the subroutine and the starting folder to process all the subfolders.

Upvotes: 0

Related Questions