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

Actually there was a reason for nodirs(). I don't want to find recursively. I'm only looking for files in the specified directory, I'm not interested in anything in its sub directories if they exist
find( { wanted => \&get_files, preprocess => \&nodirs }, "$BASEDIR/$di +r" ); sub nodirs { grep !-d, @_; }
I'll have to add that part back in

Replies are listed 'Best First'.
Re^8: Help with $File:Find ( rule )
by beech (Parson) on Feb 21, 2018 at 05:13 UTC

    Hi,

    If you don't want to recurse , use maxdepth 1, not preprocess

    findrule is just about the easiest way to use File::Find

    $ findrule test test test/foo.txt test/test test/test/foooooo.txt $ findrule test -file test/foo.txt test/test/foooooo.txt $ findrule test -file -name "foo*txt" test/foo.txt test/test/foooooo.txt $ findrule test -file -name "foo*txt" -maxdepth 0 $ findrule test -file -name "foo*txt" -maxdepth 1 test/foo.txt $ findrule test -file -name "foo*txt" -maxdepth 2 test/foo.txt test/test/foooooo.txt

    setup/teardown

    perl -MPath::Tiny -le " path(q{test/test/foooooo.txt})->touchpath " perl -MPath::Tiny -le " path(q{test/foo.txt})->touchpath " perl -MPath::Tiny -le " path(q{test})->remove_tree "
Re^8: Help with $File:Find
by Marshall (Canon) on Feb 21, 2018 at 02:57 UTC
    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?

      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