getting list of directories, returned as an array 
Author Message
 getting list of directories, returned as an array

Hello, I have searched Dejanews, scanned through the FAQ, and looked
through CPAN.html, but have not found what I am looking for.  I would
like (or maybe just would like help writing) a subroutine to return a
list of directories to an array starting with the current directory.
And maybe an option to dictate whether to return the list as a set of
relative paths or absolute paths.  I know *nix has some tools that
could be manipulated and piped into Perl, but I am trying to write my
script to be system independent.

Any help or pointers would be great.  Code samples are great, as are
pointers to web pages.

Thanks!

j----- k-----

Please reply via e-mail as well.

Joshua J. Kugler
Computer Consultant--Web Developer
Real e-mail address spelled out to prevent spam. jkugler at inreach dot com
http://www.*-*-*.com/
Every knee shall bow, and every tongue confess, in heaven, on earth, and under the earth, that Jesus Christ is LORD -- Count on it!
- - - - -
To reply via e-mail, please remove 'FIGHT-SPAM' from my e-mail address. Thanks.



Fri, 26 May 2000 03:00:00 GMT  
 getting list of directories, returned as an array

Quote:

> I would like (or maybe just would like help writing) a subroutine to
> return a list of directories to an array starting with the current
> directory.

You could do this easily with readdir and friends, maybe along with grep
and map.

Quote:
> Please reply via e-mail as well.
> To reply via e-mail, please remove 'FIGHT-SPAM' from my e-mail address.

To get replies via email, please fix your headers. Thanks.

--
Tom Phoenix           http://www.teleport.com/~rootbeer/

Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/
              Ask me about Perl trainings!



Fri, 26 May 2000 03:00:00 GMT  
 getting list of directories, returned as an array



Quote:
> I would
> like (or maybe just would like help writing) a subroutine to return a
> list of directories to an array starting with the current directory.
> And maybe an option to dictate whether to return the list as a set of
> relative paths or absolute paths.  I know *nix has some tools that

I take it you mean a recursive list? have a look at the File::Find module.

hint: use the -d file test

Martien
--
Martien Verbruggen                  |
Webmaster www.tradingpost.com.au    | We are born {*filter*}, wet and hungry. Then
Commercial Dynamics Pty. Ltd.       | things get worse.
NSW, Australia                      |



Sat, 27 May 2000 03:00:00 GMT  
 getting list of directories, returned as an array





()
()> I would
()> like (or maybe just would like help writing) a subroutine to
return a
()> list of directories to an array starting with the current
directory.
()I take it you mean a recursive list? have a look at the File::Find
module.
()
()hint: use the -d file test

Ok, I have been able to write a little munge that does what I want,
but there has to be a better way.  Here is what I have so far.

use File::Find;

find(\&get_dir_list, '/win');


sub get_dir_list {$main::dirlist{$File::Find::dir} = 1;}

That works, and well.  The only problem is the File::Find goes through
EVERY file, even though all I want is directory name.  And I don't see
how -d could change that because File::Find passes the directory name
to the subroutine, and does not have any "command line" arguments for
only listing subdirectories.  The script I am going to be using this
in could potentially be dealing with directories that have many files
in them, and it really slows things down when parses though files,
instead of just listing the directories.

I tried a routine sent to me that used readdir, but it only found one
level down.  I suppose I could rewrite find.pm, but I am not that good
on my Perl yet.  Getting a dir listing is not hard.  I can do that.  I
guess my logic doesn't work that far yet.  Well, I always want to
learn.  I guess that's what I'm doing. :)  Thanks for all your help.
It is appreciated!

As my ISP's news server isn't always reliable, please reply via e-mail as well.

Joshua J. Kugler
Computer Consultant--Web Developer
Real e-mail address spelled out to prevent spam. jkugler at inreach dot com
http://www.cwebpages.com/jkugler
Every knee shall bow, and every tongue confess, in heaven, on earth, and under the earth, that Jesus Christ is LORD -- Count on it!



Sun, 28 May 2000 03:00:00 GMT  
 getting list of directories, returned as an array


Joshua> Ok, I have been able to write a little munge that does what I want,
Joshua> but there has to be a better way.  Here is what I have so far.

Joshua> use File::Find;

Joshua> find(\&get_dir_list, '/win');



Joshua> sub get_dir_list {$main::dirlist{$File::Find::dir} = 1;}

You're trying too hard.

    use File::Find;

    find ( {

    }, '/win' );

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,990.69 collected, $186,159.85 spent; just 264 more days

--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me



Mon, 29 May 2000 03:00:00 GMT  
 getting list of directories, returned as an array

 [courtesy cc of this posting sent to cited author via email]

I've always liked this:

    #!/usr/bin/perl -lw

    use File::Find ();

    *name = \*File::Find::name;

--tom
--

    All language designers are arrogant.  Goes with the territory... :-)



Mon, 29 May 2000 03:00:00 GMT  
 getting list of directories, returned as an array



[Posted and e-mailed]

()You're trying too hard.
()
()    use File::Find;
()
()    find ( {

()    }, '/win' );
()

You're right, I was trying too hard. :)  Now I got another sticker,
which is not a big deal, because I don't mind the workaround.

This works:

find(\&get_dir_list, '.');


And well.  I like it, and probably will use it.

This doesn't work:


It gives me:

syntax error at \utah\test.bat line 7, near ") if"
Execution of \utah\test.bat aborted due to compilation errors.

Yes, I am running on a DOS system. :)  My details are below.

Thanks for your help. You advice helped much!

Details:

486DX2/66, 12MB, 540MD HD, MS-DumbOpertaingSystem v 6.22, Win 3.1
(I am going Linux as soon as I can. :)

Perl -V:

Summary of my perl5 (5.0 patchlevel 4 subversion 2) configuration:
  Platform:
    osname=dos, osvers=djgpp, archname=djgpp
    uname=''
    hint=recommended, useposix=undef, d_sigaction=define
    bincompat3=n useperlio=undef d_sfio=undef
  Compiler:
    cc='gcc', optimize='-O2 -fomit-frame-pointer -malign-loops=2
-malign-jumps=2 -malign-functions=2', gccversion=2.7.2
    cppflags=''
    ccflags =''
    stdchar='char', d_stdstdio=undef, usevfork=false
    voidflags=15, castflags=0, d_casti32=undef, d_castneg=define
    intsize=4, alignbytes=4, usemymalloc=n, randbits=31
  Linker and Libraries:
    ld='ld', ldflags ='-s'
    libpth=c:/perl/lib
    libs=-lm -lc
    libc=c:/perl/lib/libc.a, so=none
    useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_none.xs, dlext=none, d_dlsymun=undef, ccdlflags=''
    cccdlflags='', lddlflags=''

Characteristics of this binary (from libperl):
  Locally applied patches:
        DOS_DJGPP_2 - patch for the dos/djgpp port
  Built under dos
  Compiled at Oct  5 1997 18:37:11
  %ENV:
    PERLLIB_PREFIX="f:/perllib c:/perl"

    c:/perl/lib/perl5
    c:/perl/lib/perl5/site
    c:/perl/lib/perl5/site
    .

As my ISP's news server isn't always reliable, please reply via e-mail as well.

Joshua J. Kugler
Computer Consultant--Web Developer
Real e-mail address spelled out to prevent spam. jkugler at inreach dot com
http://www.cwebpages.com/jkugler
Every knee shall bow, and every tongue confess, in heaven, on earth, and under the earth, that Jesus Christ is LORD -- Count on it!



Wed, 31 May 2000 03:00:00 GMT  
 getting list of directories, returned as an array

[posted and e-mailed]

Quote:

> This works:

> find(\&get_dir_list, '.');


> And well.  I like it, and probably will use it.

In this one, you're passing a reference to a subroutine to find.

Quote:
> This doesn't work:


> It gives me:

> syntax error at \utah\test.bat line 7, near ") if"
> Execution of \utah\test.bat aborted due to compilation errors.

In this one, you're executing a block of code and passing the result
to find.

Try this instead:


which passes a reference to an anonymous subroutine to find.

Look at man perlref for more details.

Chipmunk



Wed, 31 May 2000 03:00:00 GMT  
 getting list of directories, returned as an array




()I would like (or maybe just would like help writing) a subroutine to
()return a list of directories to an array starting with the current
()directory.

Ok, I asked for help, and I got it.  You guys are great.  I love the
internet. :)  I better, seeing it is a good part of my line of work.
Ok, enough talk, here's the solution:

-----
#!/bin/perl
use File::Find;


-----

Now, wasn't that easy? :)  On large trees, this may be slow as it
cycles through EVERY file, directory or not.  But it works, and works



Thanks to everyone that helped out:






Thanks again guys.  Hopefully I can return the favor to others
someday.  Umm, maybe this could be put in the FAQ?

j----- k-----

As my ISP's news server isn't always reliable, please reply via e-mail as well.

Joshua J. Kugler
Computer Consultant--Web Developer
Real e-mail address spelled out to prevent spam. jkugler at inreach dot com
http://www.cwebpages.com/jkugler
Every knee shall bow, and every tongue confess, in heaven, on earth, and under the earth, that Jesus Christ is LORD -- Count on it!



Thu, 01 Jun 2000 03:00:00 GMT  
 getting list of directories, returned as an array



You probably need a 'sub' in there to create an anonymous sub reference:


Martien
--
Martien Verbruggen                  |
Webmaster www.tradingpost.com.au    | You can't have everything, where would
Commercial Dynamics Pty. Ltd.       | you put it?
NSW, Australia                      |



Thu, 01 Jun 2000 03:00:00 GMT  
 getting list of directories, returned as an array

Quote:

>use File::Find;


>-----

>Now, wasn't that easy? :)  On large trees, this may be slow as it
>cycles through EVERY file, directory or not.

I guess I still don't understand the problem. If you already knew
which entries were directories, why would you be writing a program?


Thu, 01 Jun 2000 03:00:00 GMT  
 
 [ 23 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Getting directory listing

2. problem in getting directory list and filelist through perl

3. Getting Directory Listing

4. Getting listing of directories as texts

5. Getting a directory list in cronological order

6. Script wanted to return a hypertext list of a directory's contents

7. Getting one element of array return value

8. Reading a Directory Listing into an Array

9. Storing A Directory Listing in an Array

10. Does map return a list or an array?

11. Returning a hash list (associative array)

12. Array Initialization and directory list searching

 

 
Powered by phpBB® Forum Software