Perl help: Creating a multidimensional array of subdirectories and its contents

I'm currently working with dozens of FASTA files, and I'm tired of having to manually change the filename in my Perl script.

I'm trying to write a simple Perl script that'll create a 2-dimensional array containing the name of the folders and its contents.

For example, I would like the output to look something like this:

Folder A iam.fna a.fna new.fna
Folder B to.fna perl.fna
Folder C filehandling.fna
Folder D please.fna help.fna me.fna

I would like to access these folders recursively, but I am new to perl filehandling, and I'm completely clueless where to begin. Any help will be appreciated! Thank you in advance.

Is this your entire directory structure or you need to handle a variable number of sub directories?

That'll be my entire directory. I only have fasta files in the various folders. I would like to list out the names of folders with its contents because the names of the folders are quite similar, and I'd like to list them all out.

In perl there are lot of module to handle the files.like File::stat.also there is a simple way to read a directory.We can use the option like -d,-f,-l.Using this we can check whether the given thing is a file or directory or link file.

For example I am reading my current directory which having some number of directories .Here is the simple way,

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


my $dir = ".";
opendir(DIR, $dir) or die $!;
while(defined (my $file = readdir(DIR)))
{
    if(-d "$file")
    {
         opendir(newDIR, $file) or die $!;
         print "Folder " . $file . ":\n" ;
        while(defined ($file = readdir(newDIR)))
        {
            print "$file,"; # Here you can do some specific operation on the file
        }
        print "\n\n";
        closedir(newDIR);
    }

}
closedir(DIR);

To read the get the files in current directory we can use the glob function.

my @arr=glob ".* *";

foreach(@arr)
{
    print "$_ \n";
}

Thanks

Wow, thanks! It's almost perfect! I just do not want to display Folder ".."; any way around it?

---------- Post updated at 08:55 PM ---------- Previous update was at 04:55 PM ----------

#!/usr/bin/perl

open (TXT, ">directory.txt");
print TXT "Organism\t\t\tFiles\n\n";

#In current directory, list all folders and contents

my $dir = ".";
opendir(DIR, $dir) or die $!;
while(defined (my $file = readdir(DIR)))
{
    if(-d "$file")
    {
        opendir(newDIR, $file) or die $!;

        print TXT "\n\nFolder" . $file . ": " ;
        while(defined ($file = readdir(newDIR)))
        {
            my @arr = glob".*fna";
            foreach(@arr)
            {
                print TXT "\t$_";
            }


        }

        closedir(newDIR);
    }

I've got this much. However, I haven't been able to figure out how to print out the contents of each folder to the txt file.

---------- Post updated at 09:01 PM ---------- Previous update was at 08:55 PM ----------

#!/usr/bin/perl

open (TXT, ">directory.txt");
print TXT "Organism\t\t\tFiles\n\n";

#In current directory, list all folders and contents

my $dir = ".";
opendir(DIR, $dir) or die $!;
while(defined (my $file = readdir(DIR)))
{
    if(-d "$file")
    {
        opendir(newDIR, $file) or die $!;

        print TXT "\n\nFolder" . $file . ": " ;
        while(defined ($file = readdir(newDIR)))
        {
            my @arr = glob "*.fna";
            foreach(@arr)
            {
                print TXT "\t$_";
            }


        }

        closedir(newDIR);
    }

I can't seem to figure out how to print out the contents of each folder (all .fna files) to the txt file.

seems shell is easier than perl, you can call shell script inside perl

for i in yourdir/*;do
if [ -d $i ];then
echo ${i##*/}" "`ls $i` | tr " " ","
echo
fi
done

Try this...

find . -type d -depth -print | grep -v ^.$ | cpio -pd /path

This command will find all subdirectories under current path and create the same under specified path

/Rajiv