in reply to Testing for existance of subdirectories

You're actually reading and testing every entry in your directory, which probably accounts for your "disk activity". Directories with more entries will obviously take longer than others, especially heavilly loaded ones. There is no built in "filter" for readdir, to the best of my knowledge, so finding directories with subdirectories is going to be involved, even in the best circumstances.

Your second solution just reads in everything and then backtracks to find subdirs. If this is faster, then a native implementation of the "dir /ad /b" command might help you out.

If you query in an overlapping way, such that you are actually testing the same directory twice, caching your findings in a hash may save you the trouble of the test.
  • Comment on Re: Testing for existance of subdirectories

Replies are listed 'Best First'.
Re: Re: Testing for existance of subdirectories
by ChrisNutting (Initiate) on Aug 19, 2001 at 14:34 UTC
    Hurrah!!! have found a solution: (Using a combination of both my attempts) This works with almost no file access at all:
    foreach my $file (readdir(CURRENT)) { next if (($file eq ".") || ($file eq "..")); my $checkDir="$dir\\$file"; Win32::File::GetAttributes($checkDir, my $attrib); if ($attrib == 16) { return 1; } }
    Not quite sure what I'm doing with that $attrib, but hey it works!! Thanks for your help guys (Needed to think in a diferent direction :)
                        ("`-''-/").___..--''"`-._
                         `6_ 6  )   `-.  (     ).`-.__.`)
                         (_Y_.)'  ._   )  `._ `. ``-..-'
                       _..`--'_..-_/  /--'_..' ,'
                     (il),-''  (li),'  ((!.-'
    

      I would change that test to:

      if ($attrib & 16) { return 1 }

      What you're doing is testing bit 4 of the file attributes, which is probably the "directory" bit. But if this was a read-only, hidden, etc directory, that $attrib would be something else, like 17.

      If that doesn't make sense, here's a hint; ask if this isn't too clear:

      You want to test this bit v 16 = 00010000b 17 = 00010001b ^
        THANK YOU!!! (You read my mind!) I just scanned my D:\ which has lots of dir's with the system bit set, and it wasn't scanning properly the depth properly. (It's working perfectly now :) (Scanned c:\windows testing every sub for subs, in 4secs)
                            ("`-''-/").___..--''"`-._
                             `6_ 6  )   `-.  (     ).`-.__.`)
                             (_Y_.)'  ._   )  `._ `. ``-..-'
                           _..`--'_..-_/  /--'_..' ,'
                         (il),-''  (li),'  ((!.-'