Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2018-09-24 08:18 GMT
Find Nodes?
    Voting Booth?
    Eventually, "covfefe" will come to mean:

    Results (191 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!