We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,414 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

get filenames from all dirs

ok i have a perl script

use strict;
use warnings;

open(my $in, ">>", "log.txt") or die "cant open log.txt $!" ;

my @files = <*>;
foreach my $file (@files){
print $in $file . "\n";
}

close $in or die "$in $!";

it gets the filenames that are in the same dir as script.

but if there is an other dir in there it needs to look in there and list those files to.

like

bla.txt
blabla.txt
bla/bla3.txt
ect

2
Contributors
3
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
4
Views
xellos
Newbie Poster
11 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
#!/usr/bin/perl
#print_files_in_subdirs.pl
use strict;
use warnings;
my $startdir = '/home/david/Programming/data';
print_files($startdir);

sub print_files {
    #This subroutine calls itself for each subdirectory it finds.
    my $dir = $_[0];
    my @files = <$dir/*>;
    my @d;
    foreach (@files) {
        next if $_ eq "." or $_ eq "..";
        my $fn = $_;
        if (-d $fn) {
            push @d, $fn;
        }
        else{
            print "$fn\n";
        }
    }
    if (scalar @d == 0) { #If no directories found, $dir is lowest dir in this branch
        return;
    }
    foreach (@d) {
        print_files($_);
    }
}
d5e5
Practically a Posting Shark
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1

Can you explain to me how $_ works?

xellos
Newbie Poster
11 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The default iterator variable in a foreach loop if no other variable is supplied.

from perldoc perlvar.

If we supply another variable, such as $fn, in the loop condition then we won't need $_. I just used it out of habit. The foreach loop can work just as well as follows.

foreach my $fn (@files) {
        next if $fn eq "." or $fn eq "..";
        
        if (-d $fn) {
            push @d, $fn;
        }
        else{
            print "$fn\n";
        }
    }
d5e5
Practically a Posting Shark
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.5206 seconds using 2.68MB