James Moser wrote:
On Dec 2, 2008, at 5:48 AM, Koti wrote:
I have a directory named "X" which has many sub directories
"Y","Z","W" and many files about 20 , and these sub directories also
contain some more sub directories and files in them and those sub
directories also contain more directories and files.
I want to read all the files that exists across all the directors,
subdirectories (only files list) and need to display .
i tried until 2 loops , but unable to complete the logic well on a
whole. can u help me if u have any idea ,
You might want to read up on recursion. Try something like this:
use warnings;
use strict;
sub directory {
my $directory = shift;
opendir(DIR, $dir) || die "Unable to open the directory";
Don't you mean:
opendir(DIR, $directory)
@contents = readdir(DIR);
close(DIR);
foreach $listitem (@contents ) {
if ( -d $listitem ) {
That won't work.
perldoc -f readdir
print " It's a directory!\n";
directory($directory.'/'.$listitem); # This is recursion...
}
else {
print $l2;
Where did $l2 come from?
print " It's a file!\n";
}
}
}
print "Enter The Directory Path";
$dir = <STDIN>;
chomp($dir);
directory($dir);
This should work for what your trying to do, but I haven't actually
tested it. I think you will get the idea.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/