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

You are both doing too much work and too little, assuming I understand what you are trying to achieve.

You don't need to open any files. You may need to recurse into sub-directories. Take a while to study the following code:

#!/usr/bin/perl use strict; use warnings; use File::Find::Rule; parseDir ('./rootdir'); sub parseDir { my ($dir) = @_; my @subdirs = File::Find::Rule->directory()->in($dir); parseDir ($_) for grep {!/^\.\.?$/ && $_ ne $dir} @subdirs; my @files = File::Find::Rule->file()->name('*.*')->in($dir); return if !@files; my $listFileName = ""; open my $listFile, '>', "$dir/List.txt" or return; print $listFile join "\n", @files, ''; }

The key to understanding it is that parseDir calls itself to deal with sub-directories. Having dealt with all sub-directories it then creates the list.txt file for the current directory.

Premature optimization is the root of all job security
  • Comment on Re: creating a list of files under each subfolder in a main directory
  • Download Code

Replies are listed 'Best First'.
Re^2: creating a list of files under each subfolder in a main directory
by Anonymous Monk on Mar 18, 2016 at 00:51 UTC

    You don't need that grep stuff

    #!/usr/bin/perl -- use strict; use warnings; use File::Find::Rule qw/ find rule /; use Path::Tiny qw/ path /; my @subdirs = rule( 'directory' , maxdepth => 1, )->in( 'startdir' ); for my $dir ( @subdirs ){ my $listFile = path( "$dir/List.txt" )->openw_utf8; rule( file => exec => sub { ## my( $shortname, $path, $fullname ) = @_; print $listFile "$_[2]\n"; return !!0; ## means discard filename }, )->in( $dir ); }
Re^2: creating a list of files under each subfolder in a main directory
by Bioinfocoder (Novice) on Mar 18, 2016 at 14:18 UTC
    Thanks a lot for the response! the issue is resolved