Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^8: Help with $File:Find

by Marshall (Canon)
on Feb 21, 2018 at 02:57 UTC ( [id://1209621]=note: print w/replies, xml ) Need Help??


in reply to Re^7: Help with $File:Find
in thread Help with $File:Find

I am very skeptical that File::Find will do what you think it does.

I figure that the preprocess routine will be run on each directory that is visited.
This preprocess routine will not limit the subdirs that will be visited.
This just organizes the order that files will be processed in current directory.

There are other File::Find type of modules which limit the "depth". Is that what you want to do?

On the other hand, if you don't want to follow into other directories, why do you even need File::Find?

Replies are listed 'Best First'.
Re^9: Help with $File:Find
by roperl (Beadle) on Feb 21, 2018 at 18:24 UTC
    I'm calling find in a sub , passing the $dir variable on each call. The nodirs sub called from "find" excludes any sub directories found within $dir. The "return unless" maybe redundant, but may also help to exclude anything that isn't a real file. I'm going to let this run for a few weeks to see if the original issue returns.
    find( { wanted => \&get_files, preprocess => \&nodirs }, "$BASEDIR +/$dir" ); sub nodirs { grep !-d, @_; } sub get_files { return unless ( -f $_ ); my $tmpfile = $File::Find::name if ( ($_) && ( (/^(?!\.).*\.($ +INTYPES)$/i) || ( (/^(?!\.).*\.($INTYPES)\.($ENGZTYPES)$/i) && !(/\.( +$OUTTYPES)\.($ENGZTYPES)$/i) ) ) ); if ( $tmpfile ) { if ( ( exists( $globalfiles{$tmpfile} ) ) && ( ( $globalfi +les{$tmpfile} ne 'submitted' ) || ( $globalfiles{$tmpfile} ne 'workin +g' ) ) ) { return; } else { if ( -e $tmpfile ) { my $lckfile = getlckfile($tmpfile); push @tmparray, $tmpfile unless ( -e $lckfile ); } } } }
      I did some more testing to try to figure this out because your reported rare symptoms are weird.

      Much to my surprise, the preprocess routine in File::Find does affect the subsequent directory traversal in File::Find.
      From the documentation, I didn't think that it would do that, but it apparently does.

      The nodirs() sub will leave the initial starting results directory in the results to be used by get_files();

      There is no need to use File::Find here because you are only looking at files in the base directory and not navigating to subdirectories.
      I would consider just using opendir (my $handle, "dirname") or die "$!" and use readdir(). I am unable to simulate a situation to reproduce your symptoms on my current computer. An unlocked data structure would explain this.

      From your code, it appears that this is a multi-process application. Right now, I am thinking that perhaps there is some rare situation where $BASEDIR/$dir doesn't exist.

      #!/usr/bin/perl use strict; use warnings; use File::Find; find( { wanted => \&get_files, preprocess => \&nodirs }, "C:/test" ); sub nodirs { grep !-d, @_; } sub get_files { #return unless ( -f $_ ); #testing without this statement # prints: # C:/test <- this is a directory nevertheless! # C:/test/SomeBogusFile.txt return unless ( -f $_ ); #testing with this statement # prints: # C:/test/SomeBogusFile.txt print "$File::Find::name \n"; } __END__ Test Directory structure C:/test SomeBogusFile.txt /subdirtest docinbsubdir.txt

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209621]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found