
Zhris
Enthusiast
Sep 11, 2012, 2:34 PM
Post #10 of 12
(3648 views)
|
Re: [eyebrowsbutt] Reading files from directory and subdirectory
[In reply to]
|
Can't Post
|
|
Hey, If you use a directory that doesn't exist then your code will exit with the "Cannot open directory" error. If all.fna is a directory that sits in the same directory as your Perl script, then you are fine. If you run with just the exit removed, is the output better than before. I re-wrote your recursive subroutine, and simplified the GC subroutine to just print the file_path. It might be a good starting point, since it only prints each file it recursively comes across. Printing too many unformatted strings can become confusing. If it works out, replace the GC subroutine with yours (don't forget to remove the final exit):
#!/usr/bin/perl use warnings; use strict; list_recursively('all.fna'); exit; sub list_recursively { my ($dir_path) = @_; opendir my $dh, $dir_path or die "cannot open dir $dir_path: $!"; foreach my $path ( map { "$dir_path/$_" } grep { !/^\.\.?$/ } readdir $dh ) { if (-f $path) { GC($path); } elsif (-d $path) { list_recursively($path); } } closedir $dh; return; } sub GC { my ($file_path) = @_; print "$file_path\n"; return; } Chris
(This post was edited by Zhris on Sep 11, 2012, 3:19 PM)
|