in reply to creating a list of files under each subfolder in a main directory

Hello, your code is very confusing: especially the nested open part. You are opening for read every file in each subdirectory and while reading them (ie for every line!) you open a file in append mode and write the content of the line to it...

Meaningfull names for variables and a correct indentation (perltidy) help a lot reading code or maintain it after a while.

If your usage of File::Find::Rule is correct (you grab first level or nested dirs?) you can try something like:

# untested foreach my $dir (@subdirs) { # if you have .. you have also . next if ($dir eq "."); next if ($dir eq ".."); my @files = File::Find::Rule->file()->name('*.*')->in($dir); # compose a filename: # you probably need a path not just a name; you have not cd to + the inside directory # many module can hadle this for you, but you can deal with a +relative path too my $outputname = "./$dir/List.txt"; # open ALWAYS a lexical FH using the 3 args form.. open my $lexical_filehandle, '>', $outputname or die "Cannot w +rite to $outputname"; # print each filename to the listfile print $lexical_filehandle "$_\n" for @files; close $lexical_filehandle; }#next dir will be processed

L*

PS you can also play with:

find . -type d -maxdepth 1 | perl -lne "system qq(ls $_ > ./$_/_list +.txt)"

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: creating a list of files under each subfolder in a main directory
by Bioinfocoder (Novice) on Mar 18, 2016 at 03:44 UTC
    Thanks a lot for your reply! I realized I didnt need to read each file in subdirectory and I edited my code based on your and other suggestion. Your code and my edited code generates list file within each subdirectory, but I am facing one issue. The array (@subdirs) creates a key for the main directory (here ./rootdir) as well. How can I ignore reading this directory in array and just read the subdirectories using File::Find? Thanks a lot!