in reply to Re^5: File:Find pattern match question
in thread File:Find pattern match question

Thanks again Ken, I have the following code, I do need to work on it a bit, at the moment it's printing out C:\Temp and nothing else. Athanasius code is printing out each of the P00 and I00 directories I made under C:\Temp for testing. I'll check out your example further, thanks

#!/usr/bin/perl # dirpathdupes use strict; use warnings; use File::Find; use Fcntl; #*****************Path Variables********************** our $wellpath = 'N:\\repos\\open\\Wells\\Regulated\\'; our $surveypath = 'N:\\repos\\open\\Surveys\\Regulated\\'; our $testpath = 'C:\\Temp\\'; #******************************************************* my %dirs; find(\&dir_names, $testpath); my @dup_dirs = grep { $dirs{$_} > 1 } keys %dirs; foreach my $l (@dup_dirs) { print "$l\n"; } sub dir_names { return unless -d; return unless /[IPD]\d{8}$/; ++$dirs{$File::Find::dir};

Replies are listed 'Best First'.
Re^7: File:Find pattern match question
by kcott (Archbishop) on Nov 03, 2013 at 05:07 UTC

    You originally had this to flag occurences:

    $dirs{$File::Find::dir} = 1;

    and this to count occurences:

    ++$dirs{$File::Find::dir};

    Because you weren't flagging the right thing, the code for that (which, as you say below, "Works perfectly! :-) Thank you very much! Cheers"), has been changed to:

    $dirs{$File::Find::name} = 1;

    Can you work out what you need to do so that you'd now be counting the right thing?

    -- Ken

      Hi Ken Thanks for your reply. Changing the sub routine to this

      sub dir_names { return unless -d; return unless /[IPD]\d{8}$/; $dirs{$File::Find::name} = 1; ++$dirs{$File::Find::name}; }
      Gives me this
      C:\Temp\hddzip>perl dirpathdupes.pl C:\Temp/Alcohol/P12345678 C:\Temp/I12345678 C:\Temp/P12345678 C:\Temp/harddrives/P12345678 C:\Temp/hddzip/P12345678
      Which seems fine as its showing all instances of the P00# but I only have one directory starting with I00#, so by all rights it should not display it if I'm seaching for dupes. Adding another P00# directory P12345677 also displays, so it seems it is still just showing directories matching "return unless /IPD\d{8}$/;"

        Every time you call dir_names(), you set $dirs{$File::Find::name} to 1, then you increment it.

        $dirs{$File::Find::name} will equal 2 after every call to dir_names()!

        -- Ken