Solved

directory listing

Posted on 2002-05-06
21
311 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
  • 9
  • 5
  • 3
  • +2
21 Comments
 
LVL 1

Expert Comment

by:sushrut
Comment Utility

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
Comment Utility
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
Comment Utility
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
 

Expert Comment

by:bedelludrica
Comment Utility
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
Comment Utility
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
Comment Utility
use File::Find;
find(sub{/[^.]/&&-d&&push@list,$File::Find::name},".");
0
 
LVL 1

Author Comment

by:GorGor1
Comment Utility
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
Comment Utility
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
Comment Utility
chdir "/starting/directory";
@list = grep{-d}<*>;
0
 
LVL 1

Expert Comment

by:japhyRPI
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Accepted Solution

by:
japhyRPI earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Note, it's not letting ALL the files in the array, but it is letting some.
0
 
LVL 1

Expert Comment

by:japhyRPI
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the help!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now