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

Thanks for your help on this one. Not sure why I thought the "wanted" routine would return more than one result. So I've removed the foreach(@array) and removed the nodirs() preprocessing
find( { wanted => \&get_files }, "$BASEDIR/$dir" ); sub get_files { return unless ( -f $_ ); my $tmpfile = $File::Find::name if ( ($_) && ( (/^(?!\.).*\.($ +INTYPES)$/i) || ( (/^(?!\.).*\.($INTYPES)\.($ENGZTYPES)$/i) && !(/\.( +$OUTTYPES)\.($ENGZTYPES)$/i) ) ) ); if ( ( exists( $globalfiles{$tmpfile} ) ) && ( ( $globalfiles{ +$tmpfile} ne 'submitted' ) || ( $globalfiles{$tmpfile} ne 'working' ) + ) ) { return; } else { if ( -e $tmpfile ) { my $lckfile = getlckfile($tmpfile); push @tmparray, $tmpfile unless ( -e $lckfile ); } } }

Replies are listed 'Best First'.
Re^7: Help with $File:Find
by roperl (Beadle) on Feb 20, 2018 at 18:52 UTC
    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

      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 "
      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 ); } } } }