Get the list of directories

Discussion in 'Perl Misc' started by Xavier MOGHRABI, Sep 23, 2004.

  1. 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

    --
    Xavier MOGHRABI - Consortium ObjecWeb
    Email : xavier.moghrabi at inrialpes.fr
    Phone : +33 4 76 61 55 25
    Xavier MOGHRABI, Sep 23, 2004
    #1
    1. Advertising

  2. Xavier MOGHRABI

    Tore Aursand Guest

    On Thu, 23 Sep 2004 10:35:09 +0200, Xavier MOGHRABI wrote:
    > 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.


    --
    Tore Aursand <>
    "What we anticipate seldom occurs. What we least expected generally
    happens." (Benjamin Disraeli)
    Tore Aursand, Sep 23, 2004
    #2
    1. Advertising

  3. Xavier MOGHRABI

    Tintin Guest

    "Xavier MOGHRABI" <> wrote in message
    news:ciu1ta$bp2$...
    > 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;
    }
    Tintin, Sep 23, 2004
    #3
  4. Xavier MOGHRABI

    Graham Wood Guest

    Tintin wrote:
    >
    > "Xavier MOGHRABI" <> wrote in message
    > news:ciu1ta$bp2$...
    > > Hello,
    > >
    > > I'm writing a perl script and I need to get the list of directories of a
    > > directory.

    >
    > 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
    Graham Wood, Sep 23, 2004
    #4
  5. Xavier MOGHRABI

    Paul Lalli Guest

    Xavier MOGHRABI wrote:

    > 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
    Paul Lalli, Sep 23, 2004
    #5
  6. Xavier MOGHRABI

    Tintin Guest

    "Graham Wood" <> wrote in message
    news:...
    > Tintin wrote:
    >>
    >> "Xavier MOGHRABI" <> wrote in message
    >> news:ciu1ta$bp2$...
    >> > Hello,
    >> >
    >> > I'm writing a perl script and I need to get the list of directories of
    >> > a
    >> > directory.

    >>
    >> 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.


    Whoops. Yes, I meant -d
    Tintin, Sep 23, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel Finkel
    Replies:
    0
    Views:
    482
    Joel Finkel
    Sep 12, 2003
  2. Jeffry van de Vuurst
    Replies:
    2
    Views:
    511
    Jeffry van de Vuurst
    Jul 30, 2003
  3. =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=

    Multiple bin-directories with virtual directories?

    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=, Nov 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    812
    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=
    Nov 9, 2004
  4. Paul F
    Replies:
    4
    Views:
    620
    Mark Fitzpatrick
    Jun 25, 2008
  5. Adam Petrie
    Replies:
    8
    Views:
    290
    Adam Petrie
    Oct 11, 2004
Loading...

Share This Page