Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Printing the Contents of Multiple Text Files from a Directory

by cjf (Parson)
on Jun 16, 2002 at 16:21 UTC ( #174932=note: print w/replies, xml ) Need Help??

in reply to Print contents of a range of text files to browser (was: text files)

Assuming the files aren't delimited records and you just want to print them out line by line. You first open the directory, grab all the text files into an array.

opendir DIR, "./" or die "Can't open: $!"; my @files = grep { /\.txt\z/ } readdir DIR; closedir DIR;

Then print your header, loop over the array, open the text files, and print their results to the browser.

print "Content-type: text/html\n\n"; foreach my $file (@files) { open FILE, $file or die "Can't open file: $!"; while (<FILE>) { print $_; } }

That's a very basic way to do it. You'll probably also want to add in some formatting (exercise left to the reader :).

Update: Oops, I missed part of the question. You'll have to prompt for input, or accept the range of the text files first (do you want to do this via a form or the command line?). Then select only those files in the number range submitted.

Update 2: ++ to Screamer for pointing out the error below. I updated the example accordingly.

Replies are listed 'Best First'.
Re^2: Printing the Contents of Multiple Text Files from a Directory
by Aristotle (Chancellor) on Jun 16, 2002 at 19:47 UTC
    Careful. That should be grep { /\.txt\z/ } readdir DIR;
    Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://174932]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2017-05-29 04:53 GMT
Find Nodes?
    Voting Booth?