directory listing

I'm trying to store a directory listing into an array.  However, I ONLY want the list of directories under the working directory to be stored in the array.  I don't want ".", "..", or ANY individual files that may be in the working directory to be stored in the array. ONLY directory names.  I can't seem to find the answer anywhere.  This is going to run on Win2k if that makes any difference.  Thanks!
LVL 1
GorGor1Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
japhyRPIConnect With a Mentor Commented:
GAH, I apologize for my erroneous post.  This is corrected:

  opendir DIR, "< $path" or die "can't opendir $path: $!";
  while (defined(my $file = readdir DIR)) {
    push @directories, "$path/$file" if
      $file ne '.' and $file ne '.. ' and -d "$path/$file";
  }
  closedir DIR;
0
 
sushrutCommented:

opendir (REQUESTDIRECTORY ,"/path/path/DIR_TO_CHECK"  ) or die "cannot open Request Directory $!\n";
@allfiles_temp  = grep {!/^\./} readdir(REQUESTDIRECTORY);
close  (REQUESTDIRECTORY );
for ($i=0 ; $i <= $#allfiles_temp ; $i++)
{
#print "$allfiles_temp[$i] \n" if ( -f $allfiles_temp[$i] );
unless ( -f $allfiles_temp[$i] )
{
print "$allfiles_temp[$i] \n";
}
}

This code will not show . and .. directories.
if you need those, you can replace reddir line
@allfiles_temp  = grep {!/^\./} readdir(REQUESTDIRECTORY);

with
@allfiles_temp  = readdir(REQUESTDIRECTORY);



0
 
bedelludricaCommented:
opendir FOLDER, 'c:\perl';
@folders1=readdir FOLDER;

foreach ( @folders1) {

 unless ($_=~/\./){

push (@folders,$_)
}


}


foreach (@folders) {


print "$_\n";

}


closedir folder;

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
sushrutCommented:
bedelludrica ,
it is a common practive in EE to give comment isnted of answer.
User can accept comment as answer.
May be you could have used comment.
Even top experts like  maneshr and  ozo tend to comment insted of answer.

You are pushing the question in 'Locked Questions' where GorGor1 may miss comments from other experts.



0
 
bedelludricaCommented:
I am sorry. I am new here and i am form spain and don`t understand english fluenty.

sorry.
0
 
GorGor1Author Commented:
bedelludrica, I rejected your answer because when I ran the code at a command prompt, the script didn't respond.  I had to hit <ctrl>-C to escape

sushrut, I like your answer.  However, it appears that the files in the directory are also being added to the array, such as 'index.html' along with others.  I only want the sub-directories to be added to the array, not any files.  Any ideas?
0
 
ozoCommented:
use File::Find;
find(sub{/[^.]/&&-d&&push@list,$File::Find::name},".");
0
 
GorGor1Author Commented:
Ozo:

Sorry, I guess I wasn't very clear and that's my fault.  I want the sub-directories to be added to the array but not all the sub-directories of the sub-directories.  Only the initial sub-directiries of the main directory.  Also, when I print out the array (print "print @dirList";) using your code, the directory names are all preceeded by './' (i.e. the directory 'directory' is added to the array as './directory') (no single quotes of course).  How do I get rid of that?  Not to mention it adds all the sub-directories of the sub-directories as mentioned.  Could you help tweak this code just a bit?  Thanks again.
0
 
GorGor1Author Commented:
also, ozo, how do I specify the starting directory using your code?  It isn't too obvious to me.
0
 
ozoCommented:
chdir "/starting/directory";
@list = grep{-d}<*>;
0
 
japhyRPICommented:
Here's a very simple chunk of code to do what you asked.

  opendir DIR, "< $path" or die "can't opendir $path: $!";
  while (defined(my $file = readdir DIR)) {
    push @directories, "$path/$file"
      if -d "$path/$file";
  }
  closedir DIR;
0
 
sushrutCommented:
GorGor1,

Here is the part of the code that should not allow index.html to be printed. I ahve tested it on winnt4 and AIX.

unless ( -f $allfiles_temp[$i] )
{
print "$allfiles_temp[$i] \n";
}

I have tested it on win NT4 and works fine.
if you want array then you can use
unless ( -f $allfiles_temp[$i] )
{
push @directories, "$allfiles_temp[$i]";
# Do something with directory name
}
 

Actually my code contained
#print "$allfiles_temp[$i] \n" if ( -f $allfiles_temp[$i] );

Sorry for this comment. You should delete it.


0
 
GorGor1Author Commented:
Wow, a bunch of solutions.  I'll try them all out and choose one when I get home from work.  Thanks guys!
0
 
GorGor1Author Commented:
sushrut,

It still seems to be letting file extensions sneak through.  The directory I used contains .doc, .xls, .dwg, etc, and the script is adding files to the array along with directories.  Here's the script:

#!c:\perl -w
use strict;

my $i;
my @dirList;

opendir (REQUESTDIRECTORY ,"h:/projects"  ) or die "cannot open Request Directory $!\n";
my @allfiles_temp  = grep {!/^\./} readdir(REQUESTDIRECTORY);
close  (REQUESTDIRECTORY );
for ($i=0 ; $i <= $#allfiles_temp ; $i++)
{
unless ( -f $allfiles_temp[$i] )
{
push @dirList, "$allfiles_temp[$i]";
}
}


for($i=0; $i <= $#dirList ; $i++)
{
     print "$dirList[$i]\n";
}

exit;


I'll continue to try others' comments too.
0
 
GorGor1Author Commented:
Note, it's not letting ALL the files in the array, but it is letting some.
0
 
japhyRPICommented:
GorGor - have you looked at my response?  The reason the code is failing is because readdir() only returns the NAMES of the files, not the full paths to them.  So you're not checking for `-f "h:/projects/foo"', you're checking for `-f "foo"' which probably isn't what you're expecting.

Also, why use a C-style for loop when a Perl loop is so much nicer on the eyes?

  for (@dirList) { print "$_\n" }

0
 
GorGor1Author Commented:
japhyRPI,

I tried using your code, but it keeps giving me the error message 'can't opendir h:/projects: No such file or directory at script.pl line 9'.

I know that's not true because I'm looking at the directory.  Can you see why it's not working??

#!c:\perl -w
use strict;

my $path = "h:/projects";
my @directories;

opendir DIR, "< $path" or die "can't opendir $path: $!";
 while (defined(my $file = readdir DIR))
{
   push @directories, "$path/$file" if $file ne '.' and $file ne '.. ' and -d "$path/$file";
}
closedir DIR;

exit;
0
 
japhyRPICommented:
Oh, geez.  I can't believe I put that < in there.  I'm sorry, but I should not have included the "<" in the opendir() call.

  opendir DIR, $path or die "can't opendir $path: $!";

Sorry about that mistake.
0
 
GorGor1Author Commented:
I got it working japhyRPI.  I had to take the '<' out of the opendir parameters.  I don't have my book in front of me and I can't remember what the '<' does.  If you see anything wrong with any of my code, please let me know.  But for now, I'll accept your comment since it's working great.

Thanks for all the help guys!

The code:

#!c:\perl -w
use strict;

my $path = "h:/projects";
my @directories;

opendir(DIR, "$path") || die "can't opendir $path: $!";
 while (defined(my $file = readdir DIR))
{
   push @directories, "$file" if $file ne '.' and $file ne '..' and -d "$path/$file";
}
closedir DIR;

for(@directories)
{
     print "$_\n";
}

exit;
0
 
japhyRPICommented:
There's no need for you to put $path or $file in quotes:

  opendir(DIR, $path) || die "can't opendir $path: $!";
  # ...
  push @directories, $file if ...

As for the "<", that's for open(), not opendir().
0
 
GorGor1Author Commented:
Thanks for the help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.