readdir/File::Find::Rule is not reading a subdirectory and its contents in perl
readdir/File::Find::Rule is not reading a subdirectory and its contents in perl
I have tried 3 different ways to read contents of a folder and none of them are able to identify a subdirectory in a setup.
Strange part is, when i recreate the folder structure locally , the sub-directory is identified and i get the file i am looking for with all the 3 solutions.
This sub directory is created on the fly along with other files in the folder in the setup. Every time i am able to read all the files which are created but not the sub directory and its contents.
I tried below solutions.
Solution 1:
use File::Find::Rule;
my $dir = '.';
my @subdirs = File::Find::Rule->directory->in($dir);
foreach (@subdirs) {
print "Dir --> $_ n";
}
my @list = ("*.txt", "*.rex");
my @files = File::Find::Rule->file()->name(@list)->in(@subdirs);
foreach (@files) {
print "File --> $_ n";
}
--> It does not list the sub directory. The sub-directory contains the file i am looking for. So i am not getting the files.
Variant of solution 1, which directly looks in the folder.
my $dir = getcwd();
my @types = ("*.txt","*.rex");
my @files = File::Find::Rule->file()->name(@types)->in("$dir");
print join("n", @files);
This also does not print the files, as i see it does not check the sub directory which has the files.
Solution 2:
my $cwd = getcwd();
sub find_rex {
my $f = $File::Find::name;
if ($f =~ /rex$/){
print "$f n";
}
}
find (&find_rex, $cwd);
Solution 3:
my @dirlist = '.'; # current dir, or command line arguments
foreach (@dirlist) { &check_dir($_); }
exit 0;
sub check_dir {
my $dir=shift;
print "Dir to search --> $dir n";
warn "cannot traverse directory $dirn"
unless (opendir D,$dir);
my @files = map {$dir.'/'.$_} grep {!m/^.{1,2}$/} readdir D;
closedir D;
foreach (@files) {
if (-d $_) {
&check_dir($_);
}
elsif (-f $_) {
if ($_ =~ /.rex$/ ){
print "Filename --> $_ n";
}
}
}
}
All these solutions worked locally for me to get the contents of the sub directory. I ensured the sub-directory had the same permissions locally also to test my code.The solutions work locally but it does not work in the actual setup.
I have run out of ideas. I am able to see the sub folder and the files in it i need when i list them in linux. I have tried the glob as well, but it also does not work.
More details: OS: Suse Linux , Bash/TCSH shell
Can anyone suggest something that i can try. I am not sure whether its a readdir problem or something else.
Has anyone faced this type of strange problem. What could i be doing wrong?
Please do suggest what i can do?
The sub folder which does not get recognized is something like this
2018-07-29T22.57.52
This folder contains the files i am looking for and Perl Modules Find and readdir does not seems to be checking this.
Please do let me know if i need to rephrase my question.
->extras({ follow => 1 })
Its not a symlink, i checked that. Its a normal directory. I will try with the option you mentioned.
– Kris
55 mins ago
It did not work. I tried with my @files = File::Find::Rule->extras({ follow => 1 })->file()->name(@types)->in("$dir");
– Kris
35 mins ago
1 Answer
1
The current work dircetory isn't what you are expecting it to be. If you want to search the directory in which the script is located, replace
my $dir = '.';
with
use FindBin qw( $RealBin );
my $dir = $RealBin;
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Is your directory a symlink? Does
->extras({ follow => 1 })
help?– Borodin
1 hour ago