Gossamer Forum
Home : General : Perl Programming :

open directory and print content of each file?

Quote Reply
open directory and print content of each file?
Hello,

I need to open a directory (linkfiles) and open a series of text files within the directory and print the content of each file one by one in the browser.

Here is what I have so far:

opendir (DIR, "linkfiles");
@files = grep { /.txt/ } readdir(DIR);
close (DIR);

$count = 0;

foreach $txt(@files) {
$count++;
open(DATA,"linkfiles");
@files = <DATA>;
close(DATA);
#how do you I print the content of a file???
}

thanks in advance.


Quote Reply
Re: open directory and print content of each file? In reply to
Code:

print $txt;


Regards,

Eliot Lee
Quote Reply
Re: open directory and print content of each file? In reply to
Eliot,

Thanks for a fast response. I already tried that. It just prints the name of the file instead of the "content" of the file. I have html tables in each file within that directory and am using the above code within another script.

Quote Reply
Re: open directory and print content of each file? In reply to
Try:

Code:

print DATA $txt;


Regards,

Eliot Lee
Quote Reply
Re: open directory and print content of each file? In reply to
Eliot, the name of each text file is stored in $txt and not it's value.

He will need to use

print @files;

Paul
Installations:http://wiredon.net/gt/
Support: http://wiredon.net/forum/

Quote Reply
Re: open directory and print content of each file? In reply to
don't slurp it

open(DF, $txt) or die "Error: $!\n";
print while (<DF>);
close(DF);

-g


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: open directory and print content of each file? In reply to
cgiclemons,

Is this what it is supposed to be? This does not print anything.


opendir (DIR, "linkfiles");
@files = grep { /.txt/ } readdir(DIR);
close (DIR);

$count = 0;

foreach $txt(@files) {
$count++;
#open(DATA,"linkfiles");
#@files = <DATA>;
#close(DATA);
open(DF, $txt) or die "Error: $!\n";
print while (<DF>);
close(DF);

}


thank you

Quote Reply
Re: open directory and print content of each file? In reply to
try setting your directory name in a variable:

Code:
$dir = 'linkfiles';
opendir (DIR, $dir) or die "Error opening directory: $!\n";
foreach (grep { /\.txt$/ } readdir(DIR)) {
open(DF, "$dir/$_") or die "Error: $!\n";
print while (<DF>);
close(DF);
}
close (DIR);
-g


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: open directory and print content of each file? In reply to
Thanks, I did not try this but I solved my problem in a different way.