reading directories 
Author Message
 reading directories

I wrote a simple script to recursively read through a directory for all the
files in all the folders but I seem to either have a problem in my code or
there is a limit of 1499 lines that perl can write in a simple text file. I
know that there is probably modules to do this for me but what is the fun in
that, especially if your a newbie like me, besides i need the practice. If
anybody could help me i would really appreciate it.

here is me code:

use Cwd;
my $cwd = cwd;
opendir (DIR, "$cwd") or die "unable to open $cwd $!";

closedir DIR;



open (FILE, ">file.txt") or die "unable to create file $!";

sub direct {


  opendir(TEMP, "$item") or die "unable to open $item $!";

  close TEMP;




 }

Quote:
}



Mon, 25 Oct 2004 09:58:35 GMT  
 reading directories

Quote:

> I wrote a simple script to recursively read through a directory for
> all the files in all the folders

Why aren't you using File::Find ?

[snip]

Quote:
> I know that there is probably modules to do this for me but what is
> the fun in that, especially if your a newbie like me, besides i need
> the practice.

One of the mottoes of perl is, "Perl makes easy things easy and
difficult things possible."

The way that perl makes easy things easy is by having modules which do
things for you.  So the best thing for a newbie to be doing is learning
to use the standard modules, such as File::Find.

Using File::Find, you could write your script as:
#!/usr/bin/perl -w
use strict;
use File::Find;
open (FILE, ">file.txt") or die "unable to create file.txt: $!";
find( sub { print FILE $_, "\n" if ! -d }, "." );
__END__

See how easy that is?

Here's a version which uses a recursive subroutine:

#!/usr/bin/perl -w
use strict;
open (FILE, ">file.txt") or die "unable to create file.txt: $!";
print FILE $_, "\n" for my_find(".");
sub my_find {
   map { opendir(DIR, $_) ? do {
      my $d = $_ . "/";
      map my_find($d.$_), sort grep !/^\.\.?\z/, readdir(DIR)

Quote:
}

__END__

To do this without building a the return list, consider:

#!/usr/bin/perl -w
use strict;
open (FILE, ">file.txt") or die "unable to create file.txt: $!";
my_find(".");
sub my_find {
   opendir(DIR, $_) ? do {
      my $d = $_ . "/";
      my_find($d.$_) for sort grep !/^\.\.?\z/, readdir(DIR)

Quote:
}

__END__

--
print reverse( ",rekcah", " lreP", " rehtona", " tsuJ" )."\n";



Mon, 25 Oct 2004 10:25:04 GMT  
 reading directories
wow, thanks for the examples now i'm going to study them.

Quote:

> > I wrote a simple script to recursively read through a directory for
> > all the files in all the folders

> Why aren't you using File::Find ?

> [snip]
> > I know that there is probably modules to do this for me but what is
> > the fun in that, especially if your a newbie like me, besides i need
> > the practice.

> One of the mottoes of perl is, "Perl makes easy things easy and
> difficult things possible."

> The way that perl makes easy things easy is by having modules which do
> things for you.  So the best thing for a newbie to be doing is learning
> to use the standard modules, such as File::Find.

> Using File::Find, you could write your script as:
> #!/usr/bin/perl -w
> use strict;
> use File::Find;
> open (FILE, ">file.txt") or die "unable to create file.txt: $!";
> find( sub { print FILE $_, "\n" if ! -d }, "." );
> __END__

> See how easy that is?

> Here's a version which uses a recursive subroutine:

> #!/usr/bin/perl -w
> use strict;
> open (FILE, ">file.txt") or die "unable to create file.txt: $!";
> print FILE $_, "\n" for my_find(".");
> sub my_find {
>    map { opendir(DIR, $_) ? do {
>       my $d = $_ . "/";
>       map my_find($d.$_), sort grep !/^\.\.?\z/, readdir(DIR)

> }
> __END__

> To do this without building a the return list, consider:

> #!/usr/bin/perl -w
> use strict;
> open (FILE, ">file.txt") or die "unable to create file.txt: $!";
> my_find(".");
> sub my_find {
>    opendir(DIR, $_) ? do {
>       my $d = $_ . "/";
>       my_find($d.$_) for sort grep !/^\.\.?\z/, readdir(DIR)

> }
> __END__

> --
> print reverse( ",rekcah", " lreP", " rehtona", " tsuJ" )."\n";



Mon, 25 Oct 2004 10:43:45 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Reading directories, entering directories, chaning file permissions...

2. Reading Directory

3. Read Directory

4. read directory question

5. Reading Directory Structure

6. help selecting and displaying specific files after reading directory:

7. fileevent and reading directories

8. Reading directories

9. Read directory in timestamp order?

10. How to read Directory Names

11. Reading Directories

12. Reading Directories w/ Perl

 

 
Powered by phpBB® Forum Software