If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > listing directories

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-16-06, 00:02
laals laals is offline
Registered User
 
Join Date: Jul 2004
Posts: 13
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
Reply With Quote
  #2 (permalink)  
Old 02-16-06, 01:22
KevinADC KevinADC is offline
Registered User
 
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.
Reply With Quote
  #3 (permalink)  
Old 02-16-06, 04:09
laals laals is offline
Registered User
 
Join Date: Jul 2004
Posts: 13
Yes

can i tell u a truth kevin



U R SOOO GREAT!!!!!!!!!!!!!!
Reply With Quote
  #4 (permalink)  
Old 02-16-06, 04:26
reneeb reneeb is offline
Registered User
 
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
Reply With Quote
  #5 (permalink)  
Old 02-16-06, 06:36
laals laals is offline
Registered User
 
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
Reply With Quote
  #6 (permalink)  
Old 02-16-06, 13:41
KevinADC KevinADC is offline
Registered User
 
Join Date: Feb 2006
Posts: 56
Quote:
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:

Quote:
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
Reply With Quote
  #7 (permalink)  
Old 02-16-06, 20:11
reneeb reneeb is offline
Registered User
 
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
Reply With Quote
  #8 (permalink)  
Old 02-16-06, 20:15
reneeb reneeb is offline
Registered User
 
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
Reply With Quote
  #9 (permalink)  
Old 02-17-06, 01:59
KevinADC KevinADC is offline
Registered User
 
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.
Reply With Quote
  #10 (permalink)  
Old 02-17-06, 05:51
reneeb reneeb is offline
Registered User
 
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On