in reply to Execution hangs on File::Find

It sounds like execution just takes a while, probably because there are a lot of files on your mapped drive, and access is slower than a local drive (I'm guessing this is a network directory?). Maybe you can log progress (eg: report on current position every 100 file?) to show that something is still happening

One obvious optimization is to only traverse the tree once. And since this would make the "wanted" function a little more complex, it's easier to read if you don't define it directly in the call to find:

{ my $lastepch = time; my @files; my @todel; my $counter; sub wanted { $logger->info("Reached $File::Find::name") if ($counter++ % 100) = += 0; push @files,$File::Find::name if (/\.*$/ and -f $File::Find::name +and stat($File::Find::name)->mtime > $lastepoch); push @todel,$File::Find::name if (-d $File::Find::name); }, $indir +; } sub Get_FileList { @files = (); @todel = (); find (\&wanted, $_[0]); $lastepch = time; ... return (\@files, \@todel); } } my ($files, $todel) = Get_FileList($indir);
I've also put the test on the file name first, it's a guess that it might be a little faster, because the name is already in memory and doesn't require drive/network access. And I'm not sure but it kind of looks like you have global variables, so I've shown you how you can have variables shared between functions without making them available everywhere.

Replies are listed 'Best First'.
Re^2: Execution hangs on File::Find
by colox (Sexton) on Dec 04, 2017 at 11:52 UTC

    thank you for the inputs. I did make the optimization you indicated in traversing only once - good find. i also added the logging within the sub routine but still indicates it is stuck on the top level directory & never traverse it. I have the same number of files locally & in the network share so im certain it is not the number of files. i was thinking i need to add network authentication on my code but then i thought mapped drive should be like local drive as i've already entered the network credentials to create the drive mapping.

      Well, add more logs to find precisely where you are stuck:

      sub wanted { $logger->debug("Reached $File::Find::name"); $logger->info(Carp::longmess("TRACE2")); if (-f $File::Find::name) { $logger->debug("This is a file: checking time"); stat($File::Find::name)->mtime > $lastepoch or return; push @files,$File::Find::name; } else { $logger->debug("Not a file"); if (-d $File::Find::name) { $logger->debug("This is a directory"); push @todel,$File::Find::name; } else { $logger->debug("Not a directory"); } } $logger->debug("Exiting"); }
      BTW, /\.*$/ means that the name must have 0 or more dots at the end. This is true for all strings so this test is useless.