G'day propellerhat,

Welcome to the Monastery.

I created some directories and populated them with files having varying contents and permissions:

$ for i in a b c; do cd $i; echo "DIR: `pwd`"; ls -l; cd ..; done DIR: /home/ken/tmp/pm_11135362/a total 1 -rw-r--r-- 1 ken None 0 Jul 25 16:44 empty ---------- 1 ken None 18 Jul 25 16:45 no_access DIR: /home/ken/tmp/pm_11135362/b total 1 -r--r--r-- 1 ken None 20 Jul 25 16:49 read_only DIR: /home/ken/tmp/pm_11135362/c total 1 -rw-r--r-- 1 ken None 7 Jul 25 16:51 read_write

I then wrote the following script which does various checks. The else blocks (with "OK to READ/WRITE") are where you'd call your reading/writing routines.

#!/usr/bin/env perl use strict; use warnings; use Cwd; use File::Find; my $cwd = getcwd(); my @dirs = map "$cwd/$_", qw{a b c}; print "--- READING ---\n"; find(\&wanted_to_read, @dirs); print "--- WRITING ---\n"; find(\&wanted_to_write, @dirs); sub wanted_to_read { if (! -f $File::Find::name) { print "$File::Find::name is not a normal file.\n"; } elsif (-z _) { print "$File::Find::name is zero-length.\n"; } elsif (! -r _) { print "$File::Find::name is not readable.\n"; } else { print "OK to READ: $File::Find::name\n"; } return; } sub wanted_to_write { if (! -f $File::Find::name) { print "$File::Find::name is not a normal file.\n"; } elsif (! -r _) { print "$File::Find::name is not readable.\n"; } elsif (! -w _) { print "$File::Find::name is not writable.\n"; } else { print "OK to WRITE: $File::Find::name\n"; } return; }

A sample run outputs:

ken@titan ~/tmp/pm_11135362 $ ./pm_11135362_file_find_example.pl --- READING --- /home/ken/tmp/pm_11135362/a is not a normal file. /home/ken/tmp/pm_11135362/a/empty is zero-length. /home/ken/tmp/pm_11135362/a/no_access is not readable. /home/ken/tmp/pm_11135362/b is not a normal file. OK to READ: /home/ken/tmp/pm_11135362/b/read_only /home/ken/tmp/pm_11135362/c is not a normal file. OK to READ: /home/ken/tmp/pm_11135362/c/read_write --- WRITING --- /home/ken/tmp/pm_11135362/a is not a normal file. OK to WRITE: /home/ken/tmp/pm_11135362/a/empty /home/ken/tmp/pm_11135362/a/no_access is not readable. /home/ken/tmp/pm_11135362/b is not a normal file. /home/ken/tmp/pm_11135362/b/read_only is not writable. /home/ken/tmp/pm_11135362/c is not a normal file. OK to WRITE: /home/ken/tmp/pm_11135362/c/read_write

I don't know what your reading/writing requirements are. See the open function, in the first instance, if you're unsure about that. Feel free to ask further questions about that if need be.

I also don't know what you mean by "IF check". It's not mentioned in the File::Find documentation. By itself, "IF" has a number of potentially valid interpretations in the context of your question (for instance, in "What does IF stand for?"); and you give no indication of what you intend to check. I've used a number of "file tests" which is possibly the sort of thing you want. [See "How do I post a question effectively?" for information on how you can help us to help you.]

Be very careful with specifying directories when using File::Find. I used Cwd for my example code, but that would have various problems in a production environment. The FindBin module may be useful if your target directories are always located relative to your script. Better options are to get the directories from a known source; e.g. a database, config file, or the like.

— Ken


In reply to Re: file modifications using file::find by kcott
in thread file modifications using file::find by propellerhat

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.