Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2004
    Posts
    13

    Unanswered: listing directories

    hi all..
    i want to list all the directories in one directory(not file only directory)
    means i want to open one directory and list all the directories in it
    ne suggections are welcome nd helpful to me
    Lal

  2. #2
    Join Date
    Feb 2006
    Posts
    56
    there are a few ways to do this, here is one:

    Code:
    chdir('path/to/dir') or die "$!";
    opendir(DIR,'.') or die "$!";
    my @folders = grep { -d } readdir(DIR);
    close DIR;
    print "$_\n" for sort {lc$a cmp lc$b} @folders;
    if you don't care about sorting the list remove the sort block:

    print "$_\n" for @folders;

    next time you should post any code you have already tried.

  3. #3
    Join Date
    Jul 2004
    Posts
    13

    Yes

    can i tell u a truth kevin



    U R SOOO GREAT!!!!!!!!!!!!!!

  4. #4
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    I would recommend to avoid chdir(). This can cause Problems when you are not aware of going back.

    Example:
    You have this structure:
    Code:
    ./script.pl
    ./test.dat
    ./t/dir1/
    ./t/dir2/
    ./t/dir3/
    And you use this code:
    Code:
    chdir('./t') or die "$!";
    opendir(DIR,'.') or die "$!";
    my @folders = grep { -d } readdir(DIR);
    close DIR;
    print "$_\n" for sort {lc$a cmp lc$b} @folders;
    
    #[...] some more code, maybe 200 lines of code
    
    open(FILEHANDLE,"<./test.dat") or die $!;
    my @lines = <FILEHANDLE>;
    close FILEHANDLE;
    This won't work because you are in ./t and there is no test.dat

    So I would recommend to use this:
    Code:
    opendir(DIR,'./t') or die "$!";
    my @folders = grep { -d } readdir(DIR);
    close DIR;
    print "$_\n" for sort {lc$a cmp lc$b} @folders;
    And even better to use
    Code:
    use FindBin ();
    ...
    Code:
    use FindBin ();
    my $dir = $FindBin::Bin . '/t';
    opendir(DIR,$dir) or die "$!";
    my @folders = grep { -d } readdir(DIR);
    close DIR;
    print "$_\n" for sort {lc$a cmp lc$b} @folders;
    board.perl-community.de - The German Perl-Community

  5. #5
    Join Date
    Jul 2004
    Posts
    13

    again

    hi
    thnks for spending ur valuable time for me
    the problem is i want to remove the . and .. directories
    ne idea???????
    Lals

  6. #6
    Join Date
    Feb 2006
    Posts
    56
    I would recommend to avoid chdir(). This can cause Problems when you are not aware of going back.
    Well, it could be a problem if you aren't aware of going back. Many things are problems if you are unaware of something. If you want to be thoruogh:

    If there are two modules using FindBin from different directories under the same interpreter, this won't work. Since FindBin uses a BEGIN block, it'll be executed only once, and only the first caller will get it right. This is a problem under mod_perl and other persistent Perl environments, where you shouldn't use this module.
    Which is why I shy away from using FIndBin

  7. #7
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Quote Originally Posted by laals
    hi
    thnks for spending ur valuable time for me
    the problem is i want to remove the . and .. directories
    ne idea???????
    Lals
    In my example...
    Code:
    my @folders = grep { -d $dir.'/'.$_ && $_ !~ /^\.?\.$/} readdir(DIR);
    This is a regular expression. If you want more information about RegEx, you should read
    perldoc perlre
    perldoc perlretut
    perldoc perlrequick
    board.perl-community.de - The German Perl-Community

  8. #8
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Quote Originally Posted by KevinADC
    Well, it could be a problem if you aren't aware of going back. Many things are problems if you are unaware of something. If you want to be thoruogh:



    Which is why I shy away from using FIndBin
    That's right, but I've seen even a lot of experienced programmers who did not recognize that they have used a chdir 200 lines before...

    As a workaround for FindBin, you can use File::Basename and the token __FILE__....
    board.perl-community.de - The German Perl-Community

  9. #9
    Join Date
    Feb 2006
    Posts
    56
    Your solution to an easy problem of just changing back to the original directory get more complicated. But I do not want to argue the point. You raised a valid concern when you mentioned chdir() can be a problem if the programmer is unaware of changing back and that concern stands on it's own merit regardless of the work-arounds or solutions. I thank you for pointing it out.

  10. #10
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    I don't want to argue as well. If you feel comfortable with chdir() and you know about possible difficulties, everything is ok. I just want that others (who never used chdir before) know the traps...
    board.perl-community.de - The German Perl-Community

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •