Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Reading Files Across Directories and Sub Directories

by koti688 (Sexton)
on Dec 02, 2008 at 11:03 UTC ( #727366=perlquestion: print w/replies, xml ) Need Help??

koti688 has asked for the wisdom of the Perl Monks concerning the following question:

I have a directory named "X" which has many sub directories "Y","Z","W" and many files about 20 files. and these sub directories also contain some more sub directories and files extra

check the Attached screen shot .What i need is , i want to read all the files that exists in directories , subdirectories and need to display .
i tried until 2 loops , but unable to complete the logic well.

This is my code
print "Enter The Directory Path"; $dir = <STDIN>; chomp($dir); opendir(DIR, $dir) || die "Unable to open the directory"; @contents=readdir(DIR); foreach $listitem ( @contents ) { if ( -d $listitem ) { opendir(DIR,$listitem) @c2=readdir(DIR); foreach $l2(@c2) { if (-d $l2) { print $l2; print " It's a directory!\n"; } else { print $l2; print " It's a file!\n"; } } } print $listitem; print " It's a directory!\n"; } else { print $listitem; print " It's a file!\n"; } } close(DIR);

Can you please tell me how to proceed to read into an array or variable and display all the files across Directories and Subdirectories .

Replies are listed 'Best First'.
Re: Reading Files Across Directories and Sub Directories
by dHarry (Abbot) on Dec 02, 2008 at 11:07 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Reading Files Across Directories and Sub Directories
by cdarke (Prior) on Dec 02, 2008 at 12:13 UTC
    When those esteemed monks reply as they do you should take note. There is always more than one way to do it, although there is not always a good reason. If you really must reinvent the File::Find wheel:
    use strict; use warnings; sub search_dir { my ($dir) = @_; my $dh; if ( !opendir ($dh, $dir)) { warn "Unable to open $dir: $!\n"; return; } # Two dummy reads for . & .. readdir ($dh); readdir ($dh); while (my $file = readdir ($dh) ) { my $path = "$dir/$file"; # / should work on UNIX & Win32 if ( -d $path ) { print "Directory $path found\n"; search_dir ($path); } else { print "File $path found\n"; } } closedir ($dh); } ########################################################## print "Enter The Directory Path"; my $dir = <STDIN>; chomp($dir); search_dir ($dir);

      Probably better to explicitly ignore "." and ".." than to blithely do two dummy reads; I'm not aware of anything that mandates that those are the first results returned by a readdir (then again I don't know of any offhand where it isn't either, just it seems more portable to ignore them on the off chance and increase your portability (on the third hand, it's entirely possible that said system would have not-cwd-or-parent entries which are named "." and ".." that you might not want to ignore . . . :)).
      </nitpick>

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        Root directories don't have "." and ".." in Windows, for example
      Thanks Cdarke, really helped a lot
      This one resulted in all kinds of trouble, like infinite loops :D This works like a charm on the other hand.
      sub search_dir { my ($dir) = @_; print "Entered search_dir sub, working with directory => $dir \n"; my $dh; # handle if ( !opendir ($dh, $dir)) { warn "Unable to open $dir: $!\n"; return; } my @FILES = grep { $_ ne '.' && $_ ne '..' } readdir($dh); foreach my $file (@FILES) { my $path = "$dir/$file"; if ( -d $path ) { print "Directory $path found\n"; search_dir ($path); } else { print "File $path found\n"; } } closedir ($dh); } # END of search_dir sub ############## Output: ... File /csvfiles/2015/12/22.csv found Directory /csvfiles/2015/10 found Entered search_dir sub, working with directory => /csvfiles/2015/10 File /csvfiles/2015/10/16.csv found File /csvfiles/2015/10/22.csv found Directory /csvfiles/2015/11 found Entered search_dir sub, working with directory => /csvfiles/2015/11 File /csvfiles/2015/11/16.csv found File /csvfiles/2015/11/11.csv found ...
Re: Reading Files Across Directories and Sub Directories
by vrk (Chaplain) on Apr 26, 2017 at 14:58 UTC

    How about... system('find', $dir).

    (Only half serious.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://727366]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2021-12-06 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (31 votes). Check out past polls.

    Notices?