Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to print the names of the files counted?
    primarykey
    data
    text
    <p>I am new to PERL and have gotten my first useful script to work! Now I would like to enhance it but do not understand how use the arrays to achieve my goal. I have read numerous articles and posts, but just do not understand yet.</p> <p>The script that I have working counts the number of files in a given directory of a given extension and prints the number out. What I would like it to also do is to print the filenames to a .txt file in the initial specified directory.</p> <p>Any advice or input is appreciated! I am sure I need to use an array to achieve this goal, I just do not understand how to feed the counted file names into it. I am able to print out the array list, I just need some help fillng the array up! Many thanks!</p> <p>The script in its current state:</p> <pre><code>#!usr/bin/perl use strict; use warnings; use diagnostics; use File::Find; print "\n\n"; print "This script will start at the given directory and\nrecursively count the files of a given type\n\n\n"; print "-----------------------------------------------------------\n\n\n"; print "What directory would you like to start the count?\n\nDirectory Path: "; my $dir = &lt;STDIN&gt;; #directory to begin search chomp $dir; print "\nWhat is the file extension you are searching for?\n\nFile Extension(.htm, .plx, .txt, etc.): "; my $filext = &lt;STDIN&gt;; #file extension we're searching for chomp $filext; my $count = 0; find(sub{$count++ if $File::Find::name =~ /$filext$/}, $dir); if ($count &gt; 0){ print "\n$count files counted, \n"; #display the number of files counted with the given file extension in the given directory } else { print "Couldn't find any files to count.\n"; #if no files of the given type are found in the given directory } </code></pre> <p>UPDATE:</p> <p>Thank you Wes. I see how that works now and thank you for taking the time to respond.</p> <p>For anyone who is interested, here is the final code: </p> <pre><code>#!usr/bin/perl use strict; use warnings; use diagnostics; use File::Find; print "\n\n"; print "This script will start at the given directory and\nrecursively count the files of a given type\n\n\n"; print "-----------------------------------------------------------\n\n\n"; print "What directory would you like to start the count?\n\nDirectory Path: "; my $dir = &lt;STDIN&gt;; #directory to begin search chomp $dir; print "\nWhat is the file extension you are searching for?\n\nFile Extension(.htm, .plx, .txt, etc.): "; my $filext = &lt;STDIN&gt;; #file extension we're searching for chomp $filext; my $count = 0; my @files; find(sub{ if ($File::Find::name =~ /$filext$/) { push @files, $File::Find::name; $count++; } }, $dir); if ($count &gt; 0){ print "\n\n-----------------------------------------------------------\n\n\n"; print "\n$count files counted: \n\n"; #display the number of files counted with the given file extension in the given directory foreach (@files){ print "$_\n"; } } else { print "Couldn't find any files to count.\n"; #if no files of the given type are found in the given directory } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload