Reading subdirectories

C

Colin

Hi, I am trying to use the following code to read the contents of a several
subdirectories in a single directory.

opendir(MainDir, "/home/colin/data")
while ($file = readdir(MainDir) {
print "$file\n";
opendir(SubDir, $file)
while($otherfiles = readdir(SubDir) {
print "$otherfiles\n";
}
}

Why can I not use $file to represent the directory I want to open in the
second opendir statement?
Can anyone help?

Thanks
 
J

Jürgen Exner

Colin said:
Hi, I am trying to use the following code to read the contents of a
several subdirectories in a single directory.

You are missing a
use strict;
use warning;
at the beginning of your program
opendir(MainDir, "/home/colin/data")

You are missing a semicolon in the line above
while ($file = readdir(MainDir) {

You are missing a close bracket in the line above
print "$file\n";
opendir(SubDir, $file)

You are missing a semicolon in the line above
while($otherfiles = readdir(SubDir) {

You are missing a close bracket in the line above
print "$otherfiles\n";
}
}

It is very rude to post a program that doesn't even compile and ask people
to find some semantic problem.
If your problem is how to locate and fix the syntax errors, then you should
have said so, e.g. "The following program has syntax errors and I can't
figure out how to fix them."
Why can I not use $file to represent the directory I want to open in
the second opendir statement?
Can anyone help?

Why don't you ask perl to help you:
opendir(SubDir, $file) or die "Cannot opendir $file because $!\n";

It is not nice to ask people to do a job that a machine can do much better.

jue
 
G

Glenn Jackman

Colin said:
Hi, I am trying to use the following code to read the contents of a several
subdirectories in a single directory.

opendir(MainDir, "/home/colin/data")
while ($file = readdir(MainDir) {
print "$file\n";
opendir(SubDir, $file)
while($otherfiles = readdir(SubDir) {
print "$otherfiles\n";
}
}

.... ignoring syntax errors in above code...

Why can I not use $file to represent the directory I want to open in the
second opendir statement?

because readdir does not prepend /home/colin/data.

Always check the return code of open and opendir.

You probably want:
my $maindir = '/home/colin/data';
opendir MainDir, $maindir or die "can't open $maindir: $!\n";
my @subdirs = grep { -d }
map { "$maindir/$_" }
grep { ! /^\.\.?$/ }
readdir MainDir;
foreach my $dir (@subdirs) {...}
 
B

Bob Walton

Colin said:
Hi, I am trying to use the following code to read the contents of a several
subdirectories in a single directory.


You should:

use File::Find;

for a job like that.

<buggy retyped uncompilable crap snipped>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top