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.
|