Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi, You could use the more powerful, more user-friendly Path::Iterator::Rule for iterating through directory trees and Path::Tiny for file handling. Note that you can pass an anonymous subroutine as the value of the visitor option to the iterator call, to handle the matching files as they are found.

In the following demonstration we want to move into B/ any files with .txt extension and no contents, found in any subdirectory of A/, then delete the subdirectory if empty. (You can of course use more meaningful conditions, e.g. test for file size, or age, or even matching contents. See the doc. Also see SEE ALSO in the doc for a discussion of the various tools that can be used for this task.)

use strict; use warnings; use feature 'say'; use Path::Iterator::Rule; use Path::Tiny; my $root = './1204707'; my $in = $root . '/A'; my $to = $root . '/B'; my $rule = Path::Iterator::Rule->new; $rule->file->name( qr/txt$/ ); $rule->file->empty; my $next = $rule->iter( $in, { depthfirst => 1, visitor => sub { my $path = path( shift ); my $parent = $path->parent; $path->move( $to . '/' . $path->basename ); rmdir $parent if not $parent->children; }, }); while ( defined( my $file = $next->() ) ) { say "processing $file"; } __END__

First set up the test files. We expect after running the program that there will be five empty text files in B/, and that A/bb (and subdir) will have been removed.

$ ls -goR 1204707 # File size here # | # V 1204707: total 8 drwxrwxr-x 5 4096 Dec 2 08:56 A drwxrwxr-x 2 4096 Dec 2 08:56 B 1204707/A: total 12 drwxrwxr-x 2 4096 Dec 2 08:56 aa drwxrwxr-x 3 4096 Dec 2 08:56 bb drwxrwxr-x 2 4096 Dec 2 08:56 cc 1204707/A/aa: total 20 -rw-rw-r-- 1 8 Dec 2 08:56 file1.txt -rw-rw-r-- 1 0 Dec 2 08:56 file2.txt 1204707/A/bb: total 20 drwxrwxr-x 2 4096 Dec 2 08:56 aaa -rw-rw-r-- 1 0 Dec 2 08:56 file3.txt -rw-rw-r-- 1 0 Dec 2 08:56 file4.txt 1204707/A/bb/aaa: total 8 -rw-rw-r-- 1 0 Dec 2 08:56 file7.txt 1204707/A/cc: total 20 -rw-rw-r-- 1 0 Dec 2 08:56 file5.txt -rw-rw-r-- 1 8 Dec 2 08:56 file6.dat 1204707/B: total 0

Run the program:

$ perl 1204707.pl processing ./1204707/A/aa/file2.txt processing ./1204707/A/bb/aaa/file7.txt processing ./1204707/A/bb/file3.txt processing ./1204707/A/bb/file4.txt processing ./1204707/A/cc/file5.txt

Check the directory tree after running the program:

ls -goR 1204707 1204707: total 8 drwxrwxr-x 4 4096 Dec 2 08:57 A drwxrwxr-x 2 4096 Dec 2 08:57 B 1204707/A: total 8 drwxrwxr-x 2 4096 Dec 2 08:57 aa drwxrwxr-x 2 4096 Dec 2 08:57 cc 1204707/A/aa: total 12 -rw-rw-r-- 1 8 Dec 2 08:56 file1.txt 1204707/A/cc: total 12 -rw-rw-r-- 1 8 Dec 2 08:56 file6.dat 1204707/B: total 40 -rw-rw-r-- 1 0 Dec 2 08:56 file2.txt -rw-rw-r-- 1 0 Dec 2 08:56 file3.txt -rw-rw-r-- 1 0 Dec 2 08:56 file4.txt -rw-rw-r-- 1 0 Dec 2 08:56 file5.txt -rw-rw-r-- 1 0 Dec 2 08:56 file7.txt

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re: File::Find help by 1nickt
in thread File::Find help by colox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found