Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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 <>;
    UPDATE:
    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 CGI.pm'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. ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://174931]
Approved by kappa
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2018-01-24 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (258 votes). Check out past polls.

    Notices?