Hi,


This is my first post in the forum.
I got a situation where i have to find a file in a folder based on its extension and there by use the file name and contents of the file.

Thanks for the reply.

Recommended Answers

Hi,


This is my first post in the forum.
I got a situation where i have to find a file in a folder based on its extension and there by use the file name and contents of the file.

Thanks for the reply.

If the extension of the files …

Jump to Post

All 3 Replies

Hi,


This is my first post in the forum.
I got a situation where i have to find a file in a folder based on its extension and there by use the file name and contents of the file.

Thanks for the reply.

If the extension of the files you want to use is ".txt", for example, you could do the following:

#!/usr/bin/perl
use strict;
use warnings;

my $folder = '/home/david/Programming/data';
foreach(glob "$folder/*.txt"){
    use_contents($_);
}

sub use_contents{
    my $filename = shift;
    print "$filename\n", '-' x 75, "\n";
    open my $fh, '<', $filename or die "Can't open $filename: $!";
    foreach(<$fh>){
        print;
    }
    print "\n", '-' x 75, "\n\n";
}

Hi,

Thanks for your reply i m really thank full for such a fast reply,but i have a problem the code from the post reply shows some error can't open

Is it that if the folder name in the path that we mention should not contain spaces..?

If the extension of the files you want to use is ".txt", for example, you could do the following:

#!/usr/bin/perl
use strict;
use warnings;

my $folder = '/home/david/Programming/data';
foreach(glob "$folder/*.txt"){
    use_contents($_);
}

sub use_contents{
    my $filename = shift;
    print "$filename\n", '-' x 75, "\n";
    open my $fh, '<', $filename or die "Can't open $filename: $!";
    foreach(<$fh>){
        print;
    }
    print "\n", '-' x 75, "\n\n";
}

Hi,

Thanks for your reply i m really thank full for such a fast reply,but i have a problem the code from the post reply shows some error can't open

Is it that if the folder name in the path that we mention should not contain spaces..?

That's probably it. Try adding use File::Glob qw(bsd_glob); to the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.