...

View Full Version : Pulling every .txt file from a dir



sir pannels
06-20-2002, 12:46 PM
hey there,
ok ive been working on quite a big script,
and got to part i cant work out....
the script should pull all the ,txt files form a dir and display them inbeded in html so there is one then next uisng <br> also should be alphabetical but im getting now where...the rest of the code fine..now i just need to finish it with this part of code can someone help me out here?
thanks aload
sir p

chrisvmarle
06-20-2002, 03:47 PM
I'm working on it ;)

Here's what I got:

#!/usr/bin/perl

opendir(DIR,".");
@files = sort(grep(/\.php$/,readdir(DIR)));
closedir(DIR);

print "Content-type: text/html\n\n";

$all_data = "";
foreach $file (@files) {
open(FILE,"$file") || die("Could not find $file. $!");
@lines = <FILE>;
close(FILE);
foreach $line (@lines) {
$line =~ s/\n/\<br\>/g; # optional
$line =~ s/\</&lt;/g; # optional
$all_data .= "$line";
}
$all_data .= "<br>";
}

print $all_data;

I hope it's useful to you.

Mzzl, Chris

sir pannels
06-21-2002, 01:51 AM
hey thanks
gives an error tho/
the logs say file does not exsist ...this is not true or it would be 404...any ideas?:confused:

chrisvmarle
06-21-2002, 08:15 AM
Oops, I tested it with my .php files 'cuz I didn't have any .txt files in my cgi-bin. And I found another little problem. Here is the new code, try this:

#!/usr/bin/perl

opendir(DIR,".");
@files = sort(grep(/\.txt$/,readdir(DIR)));
closedir(DIR);

print "Content-type: text/html\n\n";

$all_data = "";
foreach $file (@files) {
open(FILE,"$file") || die("Could not find $file. $!");
@lines = <FILE>;
close(FILE);
foreach $line (@lines) {
$line =~ s/\</&lt;/g; # optional
$line =~ s/\n/\<br\>/g; # optional
$all_data .= "$line";
}
$all_data .= "<br>";
}

print $all_data;

Mzzl, Chris

sir pannels
06-21-2002, 10:04 AM
ok cool,
still does the same thing....im thinking it means the dir is missing...i cant see in there where the dir is? it looks like the dir is " . " :confused:

chrisvmarle
06-21-2002, 02:26 PM
opendir(DIR,".");
"." is indeed the directory. It means "current dir", so this script opens the current dir, wich is where your script is running in.

You can change the dot to anything you want. Like "texts" to open a dir named "texts".

Mzzl, Chris

sir pannels
06-21-2002, 03:55 PM
hey Chris,
ok it works with no erros but does not display the info and I have changed it abit..still no errors but if you look at the code then test it yourself look what happens :confused:
im stumped...you got any ideas?
or anyone elsE?
cheers
heres the code for this....


print "Content-type:text/html\n\n";

parse_form();

$int =$FORM{int};
$band =$FORM{band};

sub parse_form {

print "<html><head><title>TabbersTemple.com - $int tabs for $band </title></head>\n";
print "<body bgcolor=\"#000000\" text=\"#FFFFFF\">\n";
print "<b><small><font face=\"Verdana\">$int Tabs for $band </font><br>\n";

opendir(DIR,"../tabberstemple/tabs/$int/$band");
@files = sort(grep(/\.txt$/,readdir(DIR)));
closedir(DIR);

$all_data = "";
foreach $file (@files) {
open(FILE,"$file") || die("Could not find $file. $!");
@lines = <FILE>;
close(FILE);
foreach $line (@lines) {
$line =~ s/\</&lt;/g; # optional
$line =~ s/\n/\<br\>/g; # optional
$all_data .= "$line";
}
$all_data .= "<br>";
}

print $all_data;
}

thanks
sir p:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum