Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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.


Comment on Re: Printing the Contents of Multiple Text Files from a Directory
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://174932]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-09-20 16:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls