Get the list of directories

X

Xavier MOGHRABI

Hello,

I'm writing a perl script and I need to get the list of directories of a
directory.

I tried few tests but there failed.

In fact I need this list to open the last directory created.

For the moment I've writen this but it doesn't work because in my variable
$retval I don't obtain the list of the directories :

$retval = system "ls -C $ENV{RESULT_DIR}";
@rep = split("\t",$retval);

Any help is welcome.

Thanks for you further help
 
T

Tore Aursand

I'm writing a perl script and I need to get the list of directories of a
directory.

This one could help:

#!/usr/bin/perl
#
use strict;
use warnings;
use File::Find::Rule;

my $dir = '/whatever';
my @dirs = File::Find::Rule->directory()->maxdepth( 1 )->in( $dir );
In fact I need this list to open the last directory created.

The list won't open it for you. See 'perldoc -f opendir' for that. To
sort @dirs;

my @sorted = sort {
-M $a <=> -M $b
} @dirs;

This sort isn't optimal, though...
$retval = system "ls -C $ENV{RESULT_DIR}";

No need to make this platform dependent.
 
T

Tintin

Xavier MOGHRABI said:
Hello,

I'm writing a perl script and I need to get the list of directories of a
directory.

I tried few tests but there failed.

In fact I need this list to open the last directory created.

For the moment I've writen this but it doesn't work because in my variable
$retval I don't obtain the list of the directories :

$retval = system "ls -C $ENV{RESULT_DIR}";
@rep = split("\t",$retval);

Many different ways to do this. One is:

my @dirs;

foreach my $dir (<$ENV{RESULT_DIR}/*>) {
next unless -f $dir;
push @dirs,$dir;
}
 
G

Graham Wood

Tintin said:
Many different ways to do this. One is:

my @dirs;

foreach my $dir (<$ENV{RESULT_DIR}/*>) {
next unless -f $dir;
push @dirs,$dir;
}

Isn't this adding the files rather than directories? -f is true if $dir
is a file. I think you want -d $dir.

Graham
 
P

Paul Lalli

Xavier said:
Hello,

I'm writing a perl script and I need to get the list of directories of a
directory.

I tried few tests but there failed.

In fact I need this list to open the last directory created.

For the moment I've writen this but it doesn't work because in my variable
$retval I don't obtain the list of the directories :

$retval = system "ls -C $ENV{RESULT_DIR}";
@rep = split("\t",$retval);

You should check the documentation for functions you use. In this case,
you're using system() without understanding what that function returns.
perldoc -f system
(third paragraph)

However, simply changing your program to do what you meant to do will
still result in a platform-dependent solution. Far better to use one of
the methods suggested by other respondents to this post
(File::Find::Rule or glob()) or one of several other ways, which include
File::Finder and opendir()/readdir().

Paul Lalli
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top