Traversing folders

D

Dirk

Hi,

I am new to Perl and I am trying to list all the folders within a given folder and I have the following code. I know it is simple but for some reason the program just stops and I am not seeing any error code being returned.

Does anybody have a good example of File::Find that they are willing to share. Examples are easier for me to follow.


#!/usr/bin/perl
#

use warnings;
use strict;
use File::Find;

my $path_name;
$path_name = '/test';

find sub {
return unless -d;
print "$File::Find::name\n";
},$path_name;

exit;
 
R

Rainer Weikusat

Dirk said:
I am new to Perl and I am trying to list all the folders within a
given folder and I have the following code. I know it is simple but
for some reason the program just stops and I am not seeing any error
code being returned.
[...]

#!/usr/bin/perl
#

use warnings;
use strict;
use File::Find;

my $path_name;
$path_name = '/test';

find sub {
return unless -d;
print "$File::Find::name\n";
},$path_name;

exit;

Works for me.
 
H

Hans Mulder

Hi,

I am new to Perl and I am trying to list all the folders within a given
folder and I have the following code. I know it is simple but for some
reason the program just stops and I am not seeing any error code being
returned.

#!/usr/bin/perl
#

use warnings;
use strict;
use File::Find;

my $path_name;
$path_name = '/test';

find sub {
return unless -d;
print "$File::Find::name\n";
},$path_name;

exit;

Does '/test' exist on your system?

What happens if you try $path_name = '..'; ?

-- HansM
 
R

Rainer Weikusat

Ben Morrow said:
Quoth Dirk <[email protected]>:
[...]
#!/usr/bin/perl
#

use warnings;
use strict;
use File::Find;

my $path_name;
$path_name = '/test';

find sub {
return unless -d;
print "$File::Find::name\n";
},$path_name;

exit;

You don't need to call 'exit' unless you want to exit early, or with an
error code. Falling off the end of a Perl program is the usual way to
exit successfully.

JFTR: This is not necessarily always true. For instance, the embedded
perl interpreter Nagios may use for executing plugins written in Perl
complains about plugins which didn't exit 'properly' if there is no
explicit exit statement at the end of the code.
 
D

Dirk

Thanks to everybody for their input. When I first ran the code I was running it for a folder that did not have a lot of sub-folders. When I ran it agains a very large folder I could not verify all folders and it appeared thatI had some missing folders. After being able to verify all the folders andit appears that the code did return all sub-folders.

However, I did notice that I am getting a permission error. Something aboutnot being able to change directory.

Thanks again for all the replies.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top