Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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

by Anonymous Monk
on Jun 16, 2002 at 16:06 UTC ( #174931=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can I get from a directory of files called 1.txt to 50.txt get the range between files like, give me all the items found from file 3.txt to 40.txt and print results to the browser?

edited: 16 Jun 2002 by jeffa - title change

  • Comment on Print contents of a range of text files to browser (was: text files)

Replies are listed 'Best First'.
(jeffa) Re: Print contents of a range of text files to browser (was: text files)
by jeffa (Bishop) on Jun 16, 2002 at 17:07 UTC
    Here is how i would do it (hey, this isn't homework is it?)
    use strict; use CGI qw(:header); use CGI::Carp qw(fatalsToBrowser); use File::Find; use File::Basename; my $start = 3; my $end = 40; my $dir = 'files'; @ARGV = (); die "start must be less than end" if $start >= $end; die "no dir $dir here" unless -d $dir; find sub { my $numb = (fileparse($_,'.txt'))[0]; return unless $numb =~ /^\d+$/; push @ARGV, $File::Find::name if $numb >= $start and $numb <= $end; }, $dir; die "no .txt files found in $dir" unless @ARGV; print header; print while <>;
    I should explain that File::Find works recursively, so any sub directories in the parent directory that match will be printed as well. Also, from here it is rather trivial to use's param() method to accept arguments from the cgi script. Don't forget to add -T (taint mode) and 'scrub' these arguments before using them. That i leave as an excercise. ;)


    (the triplet paradiddle with high-hat)
      Don't forget to add -T (taint mode) and 'scrub' these arguments before using them. That I leave as an excercise.

      Lesson 4, Part 2 of Ovid's CGI course has a bit on this for the interested.

      It is not homework, but thanks for your help. The text files are log files and users will have the choice to pick the usage between dates raging like the example. It will be from a form and the files will be listed on two select menus with all the file names.
Re: Printing the Contents of Multiple Text Files from a Directory
by cjf (Parson) on Jun 16, 2002 at 16:21 UTC

    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.

      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: perlquestion [id://174931]
Approved by kappa
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-07-20 05:17 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (424 votes). Check out past polls.