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.
|
|---|
| 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 | |
|
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 |