Solved

directory listing

Posted on 2002-05-06
21
316 Views
Last Modified: 2010-03-05
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!
0
Comment
Question by:GorGor1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
  • 3
  • +2
21 Comments
 
LVL 1

Expert Comment

by:sushrut
ID: 6993006

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
 

Expert Comment

by:bedelludrica
ID: 6993026
opendir FOLDER, 'c:\perl';
@folders1=readdir FOLDER;

foreach ( @folders1) {

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

push (@folders,$_)
}


}


foreach (@folders) {


print "$_\n";

}


closedir folder;

0
 
LVL 1

Expert Comment

by:sushrut
ID: 6993135
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:bedelludrica
ID: 6994771
I am sorry. I am new here and i am form spain and don`t understand english fluenty.

sorry.
0
 
LVL 1

Author Comment

by:GorGor1
ID: 6995525
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
 
LVL 84

Expert Comment

by:ozo
ID: 6995636
use File::Find;
find(sub{/[^.]/&&-d&&push@list,$File::Find::name},".");
0
 
LVL 1

Author Comment

by:GorGor1
ID: 6997442
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
 
LVL 1

Author Comment

by:GorGor1
ID: 6997444
also, ozo, how do I specify the starting directory using your code?  It isn't too obvious to me.
0
 
LVL 84

Expert Comment

by:ozo
ID: 6997719
chdir "/starting/directory";
@list = grep{-d}<*>;
0
 
LVL 1

Expert Comment

by:japhyRPI
ID: 6998073
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
 
LVL 1

Accepted Solution

by:
japhyRPI earned 50 total points
ID: 6998074
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
 
LVL 1

Expert Comment

by:sushrut
ID: 6998200
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
 
LVL 1

Author Comment

by:GorGor1
ID: 6998628
Wow, a bunch of solutions.  I'll try them all out and choose one when I get home from work.  Thanks guys!
0
 
LVL 1

Author Comment

by:GorGor1
ID: 6999065
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
 
LVL 1

Author Comment

by:GorGor1
ID: 6999070
Note, it's not letting ALL the files in the array, but it is letting some.
0
 
LVL 1

Expert Comment

by:japhyRPI
ID: 6999081
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
 
LVL 1

Author Comment

by:GorGor1
ID: 6999395
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
 
LVL 1

Expert Comment

by:japhyRPI
ID: 6999431
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
 
LVL 1

Author Comment

by:GorGor1
ID: 6999447
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
 
LVL 1

Expert Comment

by:japhyRPI
ID: 6999474
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
 
LVL 1

Author Comment

by:GorGor1
ID: 6999531
Thanks for the help!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl, group, sort, count question 6 157
Exchange 2010 Transport Rule Regex 28 156
batch script for automated email 12 142
Perl Versus AWK? 7 96
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question