Our Sponsors



Download BioinformaticsOnline(BOL) Apps in your chrome browser.




Question: Question: How to find a files in a folder or sub folders?

Manshi Raghubanshi
3908 days ago

Question: How to find a files in a folder or sub folders?

Hi Bioinformaticians, 

I am new to bioinformatics and Perl. So, please execuse me if I disturb you all with newbie question.

I am strugling to find a specific files (cattle_genome.fasta) from a folder and sub folders, and read it. I got some of the help form this website http://stackoverflow.com/questions/5651659/read-all-files-in-a-directory-in-perl in which they explain the extraction method using opendir function. It is suitable only when we have know directory name and depth of the folder.

Can you please suggest me any other method to find the file recursively and read or write the specific fasta file. 

Answers
1

In such cases you can use File::Find module (http://perldoc.perl.org/File/Find.html. For beginner PerlMonk have very nice tutorial: http://www.perlmonks.org/?node_id=217166

 

use strict;
use File::Find;

find(\&wanted, "birds_subset");

sub wanted {
    if (-f and $File::Find::name =~/cattle_genome.fasta/){  # -f flag for file, you can use -d if directory

        my $file= "cattle_genome.fasta";  ## Name of your file
        open(FILE, "$file") || (warn "Can't open \n");
        $/ = "\n";
            while () {
            chomp;
            print "$_\n"; ## do whatever want
        }
        close FILE;

}

# Warning code not tested