Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: File::Find - Traverse the first level directories from starting point - How?

by 1nickt (Canon)
on May 19, 2020 at 02:11 UTC ( [id://11116921]=note: print w/replies, xml ) Need Help??


in reply to File::Find - Traverse the first level directories from starting point - How?

Hi, welcome to the Monastery,

That tutorial, while still probably valid, is almost 20 years old. File::Find is still the core Perl module, but there are modern wrappers to make life easier.

See Path::Tiny which is modern Perl's basic file-handling tool, and which includes an iterator() method as well as a wrapper around that (visit()) which takes a callback to be executed on every file found.

You didn't show an example of your File::Find code, but I'd rather steer you towards a modern tool anyway, so here's a demo:

$ ls -d vskatusa/*/*/* vskatusa/blarg/blorg/blech vskatusa/foo/bar/baz $ perl -Mstrict -MPath::Tiny -wE 'say for path("vskatusa")->children' vskatusa/blarg vskatusa/foo $ echo 'frooble' > vskatusa/textfile $ perl -Mstrict -MPath::Tiny -wE 'say for path("vskatusa")->children' vskatusa/blarg vskatusa/textfile vskatusa/foo $ perl -Mstrict -MPath::Tiny -wE 'path("vskatusa")->visit(sub { say if + $_->is_dir });' vskatusa/blarg vskatusa/foo

For more complex file-finding use cases (which yours is not), see Path::Iterator::Rule by the same author.

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
[Soved] Re^2: File::Find - Traverse the first level directories from starting point - How?
by vskatusa (Acolyte) on May 19, 2020 at 03:20 UTC
    Thank you very much. For the benefit of others here is the code that worked.
    use Path::Tiny qw/ path cwd /; my $dir = '//192.168.1.44/somedir'; my @files = path( $dir )->children; my @myDirectories = path( $dir )->children; for my $dirName ( @myDirectories ){ if (!( $dirName =~ m/\/\./ )) { # skip dir like /xxxx/.xx print "$dirName\n"; } }

      Great, thanks for the feedback!

      A slightly more idiomatic way of writing your code:

      use strict; use warnings; use Path::Tiny; my $dir = '//192.168.1.44/somedir'; path( $dir )->visit( sub { print "$_\n" if /^[^.]/ && $_->is_dir; });

      Note that your code reads the names of all the files (plain files and directories) into two different variables; also I'm not sure about the regexp, it's not clear from your comment what names you are wanting to skip. The file/dir name at that point in the loop shouldn't have a leading slash IIUC.

      Hope this helps!

      Update: s/all the files/names of all the files/ thx soonix

      The way forward always starts with a minimal test.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-25 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found