Hi,
I am trying to create a list (.txt file) of files under each subdirectory, for all subdirectories inside a main dir
eg -
./rootdir
./rootdir/folder1 has files file1.txt, file2.txt,file3.txt..etc
another sub-folder
./rootdir/folder2 has files file1.txt, file2.txt,file3.txt..etc too
My script should create a list of files inside each subfolder, with path to all files corresponding to that subfolder like -
./rootdir/folder1/List1.txt
./rootdir/folder2/List2.txt
My code is not generating any file in the subfolders, can someone please point out the error
EDITED code - Now my code is generating files in within each subdirectory, But 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
use strict;
use warnings;
use File::Find::Rule;
my $directory = './rootdir';
my @subdirs = File::Find::Rule->directory->in( $directory );
foreach my $dir (@subdirs) {
#print "$dir\n";
next if ($dir eq "..");
if (-d $dir)
{
my $curr_dir = $dir;
open (my $OUTFILE, '>>', "$curr_dir/List.txt") or die "Cant op
+en '$dir.txt!' $!";
my @files = File::Find::Rule->file() ->name( '*.*' ) -> in( $d
+ir);
foreach $_ (@files)
{
print $OUTFILE "$_\n";
}
close $OUTFILE;
}
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.