Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

File:: Find help

8 views
Skip to first unread message

punit jain

unread,
Jan 10, 2013, 7:26:18 AM1/10/13
to begi...@perl.org
Hi,

I have a requirement where I have directory structure like : -

test --> test/user1/files/, test/user2/files/, test/user3/files/ etc.
under sub-directories with usernames I have file with name usersettings.

So the final structure as : -

test / user1 / usersettings
/files/
user2 / usersettings
/files/
user3 / usersettings
/files/
user4 / usersettings

etc

I need to get all the subdirectories of test and then read the file
usersettings under that later on to do some processing. I wrote code below
:-
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename qw(basename dirname);
use File::Find qw(find);
use File::Find::Rule;
my $indir = shift;
my $Users = {};
my @userdirs=File::Find::Rule->maxdepth(1)->directory->in($indir);

# this will give me user directories which I want only to depth 1.

foreach my $dir(@userdirs){
next if($dir eq "$indir");

# I need to skip parent directory

my $user = basename($dir);
print "$user"."\n";
find( sub {
print $File::Find::name;
if ($File::Find::name =~ /Contacts/ && -s $File::Find::name > 0
) {
print "$File::Find::name";

# do some processing
}
}, $dir);

}

However I get :-

Use of uninitialized value in print at new.pl line 21.
Use of uninitialized value in pattern match (m//) at new.pl line 22.

I think the issue is it is still using depth as 1. How do I reset it ?

Regards.

David Precious

unread,
Jan 10, 2013, 7:37:03 AM1/10/13
to begi...@perl.org
On Thu, 10 Jan 2013 17:56:18 +0530
punit jain <contactp...@gmail.com> wrote:

> I need to get all the subdirectories of test and then read the file
> usersettings under that later on to do some processing. I wrote code
> below :-
> #!/usr/bin/perl
> use strict;
> use warnings;
> use File::Basename qw(basename dirname);
> use File::Find qw(find);
> use File::Find::Rule;
[...]
> find( sub {
> print $File::Find::name;
> if ($File::Find::name =~ /Contacts/ && -s
> $File::Find::name > 0 ) {
> print "$File::Find::name";
>
> # do some processing
> }
> }, $dir);
>
> }
>
> However I get :-
>
> Use of uninitialized value in print at new.pl line 21.
> Use of uninitialized value in pattern match (m//) at new.pl line 22.

I suspect the issue is likely to be that you're mixing File::Find and
File::Find::Rule - the latter also exports a find() method, so you may
be inadvertently calling File::Find::Rule's find() method, which
doesn't set $File::Find::name.

I would recommend changing your code to simply use File::Find::Rule
exclusively. Alternatively, call File::Find::find() instead of just
find().


--
David Precious ("bigpresh") <dav...@preshweb.co.uk>
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook
www.preshweb.co.uk/cpan www.preshweb.co.uk/github


John W. Krahn

unread,
Jan 10, 2013, 8:10:56 AM1/10/13
to Perl Beginners
punit jain wrote:
> Hi,

Hello,


> I have a requirement where I have directory structure like : -
>
> test --> test/user1/files/, test/user2/files/, test/user3/files/ etc.
> under sub-directories with usernames I have file with name usersettings.
>
> So the final structure as : -
>
> test / user1 / usersettings
> /files/
> user2 / usersettings
> /files/
> user3 / usersettings
> /files/
> user4 / usersettings
>
> etc
>
> I need to get all the subdirectories of test and then read the file
> usersettings under that later on to do some processing.

That is easy enough to do:

for my $file ( <test/*/usersettings> ) {
open my $FH, '<', $file or die "Cannot open '$file' because: $!";
# process $file contents here
}


> I wrote code below
> :-
> #!/usr/bin/perl
> use strict;
> use warnings;
> use File::Basename qw(basename dirname);
> use File::Find qw(find);
> use File::Find::Rule;
> my $indir = shift;
> my $Users = {};

Why not just use a hash instead of a reference to a hash?


> my @userdirs=File::Find::Rule->maxdepth(1)->directory->in($indir);
>
> # this will give me user directories which I want only to depth 1.
>
> foreach my $dir(@userdirs){
> next if($dir eq "$indir");

perldoc -q quoting

next if $dir eq $indir;


> # I need to skip parent directory
>
> my $user = basename($dir);
> print "$user"."\n";

perldoc -q quoting

print "$user\n";
OR:
print $user . "\n";
OR:
print $user, "\n";


> find( sub {
> print $File::Find::name;
> if ($File::Find::name =~ /Contacts/&& -s $File::Find::name> 0
> ) {
> print "$File::Find::name";
>
> # do some processing
> }
> }, $dir);
>
> }
>
> However I get :-
>
> Use of uninitialized value in print at new.pl line 21.
> Use of uninitialized value in pattern match (m//) at new.pl line 22.

Which lines above are 21 and 22?



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

punit jain

unread,
Jan 10, 2013, 10:32:39 AM1/10/13
to David Precious, begi...@perl.org
I think the issue is as you mentioned exporting find from File::Find::Rule.
However File::Find I am not sure I will get that flexibility to search only
1 level depth.
> --
> To unsubscribe, e-mail: beginners-...@perl.org
> For additional commands, e-mail: beginne...@perl.org
> http://learn.perl.org/
>
>
>
0 new messages