Click here to get back home

Help with script to list non-html files in folder

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Help with script to list non-html files in folder mac 10-27-2005
Get Chitika Premium
Posted by mac on October 27, 2005, 2:00 pm
Please log in for more thread options


Hi

I have this code but it does not seem to list non-html files.

Could anyone point me in the right direction.

Thanks in advance

Mark



#!/usr/bin/perl
print "Content-type: text/html\n\n";
# Define Variables
$basedir = '../';
$baseurl = 'http://www.mywebsite.co.uk/';
@files = (*.zip');
$title = "My Web Site";

# Get Files To Search Through
&get_files;

# Print Results of Search
&return_html;

sub get_files {

chdir($basedir);
foreach $file (@files) {
$ls = `ls $file`;
@ls = split(/\s+/,$ls);
foreach $temp_file (@ls) {
if (-d $file) {
$filename = "$file$temp_file";
if (-T $filename) {
push(@FILES,$filename);
}
}
elsif (-T $temp_file) {
push(@FILES,$temp_file);
}
}
}
}

sub return_html {
print "<html>\n";
print "<head>\n";
print "<title>Directory</title>\n";
print "</head>\n";
print "<body>\n";

print "Contents of folder:<p>\n";

foreach $FILE (@FILES) {
print "<a href=\"$baseurl$FILE\">$FILE</a></p>\n";
}

print "</body>";
print "\n</html>";

}



Posted by John W. Krahn on October 27, 2005, 11:05 pm
Please log in for more thread options


mac wrote:
> Hi

Since you are not using modules or asking a question about modules what made
you decide that comp.lang.perl.modules was the correct newsgroup for this?


> I have this code but it does not seem to list non-html files.

What error or warning messages do you get? What does it "seem to list"?


> Could anyone point me in the right direction.
>
>
>
> #!/usr/bin/perl

Since this is a CGI program you should use taint mode and you should also
enable warnings and strict.

#!/usr/bin/perl -T
use warnings;
use strict;


> print "Content-type: text/html\n\n";
> # Define Variables
> $basedir = '../';
> $baseurl = 'http://www.mywebsite.co.uk/';
> @files = (*.zip');
^^
You are missing a quote.

> $title = "My Web Site";
>
> # Get Files To Search Through
> &get_files;

Why are you using a subroutine for code that will only run once? Why are you
using Perl4 style subroutine calls?


> # Print Results of Search
> &return_html;
>
> sub get_files {
>
> chdir($basedir);

You should verify that chdir() worked correctly.


> foreach $file (@files) {
> $ls = `ls $file`;

Is the ls command available? Why not use perl's built-in opendir/readdir or
glob functions?


> @ls = split(/\s+/,$ls);

What if your file names contain whitespace? Why not just use backticks in
list context?


> foreach $temp_file (@ls) {
> if (-d $file) {
> $filename = "$file$temp_file";

You are missing a directory separator between $file and $temp_file.


> if (-T $filename) {
> push(@FILES,$filename);
> }
> }
> elsif (-T $temp_file) {
> push(@FILES,$temp_file);
> }
> }
> }
> }
>
> sub return_html {
> print "<html>\n";
> print "<head>\n";
> print "<title>Directory</title>\n";
> print "</head>\n";
> print "<body>\n";
>
> print "Contents of folder:<p>\n";
>
> foreach $FILE (@FILES) {
> print "<a href=\"$baseurl$FILE\">$FILE</a></p>\n";
> }
>
> print "</body>";
> print "\n</html>";
>
> }


John
--
use Perl;
program
fulfillment


Posted by metaperl@gmail.com on October 28, 2005, 2:29 am
Please log in for more thread options


Mac,

I suggest you use File::Find, which is a stnadard part of Perl:

http://search.cpan.org/author/NWCLARK/perl-5.8.7/lib/File/Find.pm

mac wrote:
> Hi
>
> I have this code but it does not seem to list non-html files.
>
> Could anyone point me in the right direction.
>
> Thanks in advance
>
> Mark
>
>
>
> #!/usr/bin/perl
> print "Content-type: text/html\n\n";
> # Define Variables
> $basedir = '../';
> $baseurl = 'http://www.mywebsite.co.uk/';
> @files = (*.zip');
> $title = "My Web Site";
>
> # Get Files To Search Through
> &get_files;
>
> # Print Results of Search
> &return_html;
>
> sub get_files {
>
> chdir($basedir);
> foreach $file (@files) {
> $ls = `ls $file`;
> @ls = split(/\s+/,$ls);
> foreach $temp_file (@ls) {
> if (-d $file) {
> $filename = "$file$temp_file";
> if (-T $filename) {
> push(@FILES,$filename);
> }
> }
> elsif (-T $temp_file) {
> push(@FILES,$temp_file);
> }
> }
> }
> }
>
> sub return_html {
> print "<html>\n";
> print "<head>\n";
> print "<title>Directory</title>\n";
> print "</head>\n";
> print "<body>\n";
>
> print "Contents of folder:<p>\n";
>
> foreach $FILE (@FILES) {
> print "<a href=\"$baseurl$FILE\">$FILE</a></p>\n";
> }
>
> print "</body>";
> print "\n</html>";
>
> }



Posted by mac on October 29, 2005, 5:33 am
Please log in for more thread options


I realise this is the wrong group and I have posted my message in
another group.

Thanks for taking the trouble to the repy

Mark



Similar ThreadsPosted
How to package the _Inline folder contents in an exe produced using Perl. August 29, 2005, 2:43 am
Portable dot-files (hidden-files) ? November 5, 2007, 1:20 am
how do I list modules November 23, 2006, 5:09 pm
ANNOUNCE: List-Compare-0.31 August 15, 2004, 10:26 am
ANNOUNCE: List::Compare v0.31 August 22, 2004, 2:06 pm
List of all modules installed on a system August 8, 2007, 1:41 pm
Stopping the cpan shell from fetching the modules list August 10, 2006, 3:45 am
How should I announce the list of delayed class/object/method loaders? October 25, 2005, 9:54 am
List of sample Build::Module Build.PL May 7, 2007, 6:49 pm
Printing of PDF Files July 12, 2004, 4:55 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap