Should be a simple subdirectory check

Ok. Just getting back into PERL and probably (or most definitely) making a mountain out of a mole hill.

I'm trying to see if a subdirectory exists, and if not, print the slightly modified path of the missing sub to a file. Sounds simple enough. Well here is my elaborate code. Save the snickering. I know it's nuts. There must be a simpler way.

Directory structure is:

/nas/240/234/2311/VMAIL/1

I need to check that the directory /1 exists. If not, print the 2402342311 to a file.

Any help and tolerance is greatly appreciated

use strict;

my $nas = "/nas";

opendir (NAS, $nas) or die "Can't open $nas: $!";
open OUTFILE, ">bad_accounts" or die $!;

while(my $npa = readdir(NAS)) {
    if ($npa =~ /\d{3}/) {
        my $path = $nas . "/" . $npa;
        print "PATH = '$path' \n";
        opendir (NPA, $path) or die "Can't open $path: $!";
        while(my $nxx = readdir(NPA)) {
           if ($nxx =~ /\d{3}/) {
               my $path = $path . "/" . $nxx;
               print "PATH = '$path' \n";
               opendir (NXX, $path) or die "Can't open $path: $!";
               while(my $line = readdir(NXX)) {
                   if ($line =~ /\d{4}/) {
                       my $path = $path . "/" . $line;
                       print "PATH = '$path' \n";
                       opendir (LINE, $path) or die "Can't open $path: $!";
                       while(my $vmail = readdir(LINE)) {

                           if ($vmail == "VMAIL") {
                               my $path = $path . "/VMAIL";
                               opendir (VMAIL, $path) or die "Can't open $path: $!";
                               my $count = grep { ! /^\.{1,2}/ } readdir VMAIL;
                               if ($count > 2) {
                                  print "COUNT = $count \n";
                               }
                               else {
                                  print OUTFILE "BAD ACCT = " . $npa . $nxx . $line . "\n";
                                  print OUTFILE "COUNT = $count \n";
                               }
                           }
                       }
                   }
               }
           }
        }
    }
}
closedir LINE;
closedir NXX;
closedir NPA;

I do get the numbers in a file, but the each number gets printed 3 times :wall:

expecting this .. ??!!

dir_path=/nas/240/234/2311/VMAIL/1
[ -d "$dir_path" ] && echo "Exists" || echo "$dir_path" | awk -F/ '{print $3$4$5}' > outfile

Yes, that's why I'm checking for count<2 because . And .. Always exist. I failed to mention this is a rather large directory tree, so the subdirectort names will need to be variable.

My script works even though its probably overkill. Just can't figure out why it's printing each match 3 times to OUTFILE

---------- Post updated at 07:38 AM ---------- Previous update was at 07:36 AM ----------

Meant count>2

---------- Post updated at 09:05 AM ---------- Previous update was at 07:38 AM ----------

Found it. I was checking $vmail == "VMAIL" when == is for numerical conditions.