Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a dir 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I need to know how to make a perl script read a given directory, and search for *.chr files. then open all of them found one by one, get information from each one (theyre in flatfile format) print it on one row of a table, close it, go to the next and print on another row on a table.

I know how to do this if it was one file, but I need to read many files... (they contain member info and each member is a new file not just a new line in a flatfile)
 
$dir = "/home/site/opendir (DIR, $dir);
@files =
sort
grep { -f "$dir/$_" }
readdir (DIR);
closedir (DIR);
foreach $file (@files)
{
$file =~ m,.*\.(.*),;
$extention="$1";
if ($extention eq "chr"){
open(data, &quot;<$dir/$file&quot;);
while(<data>){
#do something with file
}
close(data);
}
}

Be sure $dir is permissioned to allow world read access.

773 would be good

Tony
 
hmm.... ok, I ran into another problem... your code worked.... but now I cant print contents from the file...


Im using the following code:

$dir = &quot;/home/htdocs/eqguardians/wanderinglords/char&quot;;
opendir(DIR,$dir);
@files=
sort
grep { -f&quot;$dir/$_&quot;}
readdir (DIR);
closedir (DIR);
foreach $file (@files)
{
$file=~m,.*\.(.*),;
$extention=&quot;$1&quot;;
if($extention eq &quot;txt&quot;){
open(data, &quot;<$dir/$file&quot;);
while(<data>){
#OPEN FILE AND PARSE
#print &quot;$file&quot;;
chomp($rec);
($inval,$server,$name,$status1,$level,$race,$class,$zone,$locx,$locy,$locz)=split(/\|/,$rec);
if ($status1 ne 'Offline'){
print &quot;<tr height='2'><td><font size='2'>$server</font></td><td><font size='2'>$name</font></td><td><font size='2'>$level</font></td><td><font size='2'>$race</font></td><td><font size='2'>$class</font></td><td><font size='2'>$zone</font></td><td><font size='2'>$locx</font></td><td><font size='2'>$locy</font></td><td><font size='2'>$locz</font></td></tr>\n&quot;;
}

}
close(data);
}
}


(the reason the #print &quot;$file&quot;; is there is because thats how I tested to see if your part of the script was working... which it did, since it prited the names of all the files)


any idea why this wouldnt work? I know its somewhere in this part of the code...




chomp($rec);
($inval,$server,$name,$status1,$level,$race,$class,$zone,$locx,$locy,$locz)=split(/\|/,$rec);
if ($status1 ne 'Offline'){
print &quot;<tr height='2'><td><font size='2'>$server</font></td><td><font size='2'>$name</font></td><td><font size='2'>$level</font></td><td><font size='2'>$race</font></td><td><font size='2'>$class</font></td><td><font size='2'>$zone</font></td><td><font size='2'>$locx</font></td><td><font size='2'>$locy</font></td><td><font size='2'>$locz</font></td></tr>\n&quot;;
}

}
 
ok.. I know its a problem in this somewhere..

open(data, &quot;<$dir/$file&quot;);
while(<data>){
#OPEN FILE AND PARSE
#print &quot;$file&quot;;
chomp($rec);
($inval,$server,$name,$status1,$level,$race,$class,$zone,$locx,$locy,$locz)=split(/\|/,$rec);

 
Umm, try:

$rec = $_;

followed by

chomp($rec);
...

Tim --
Tim <tim@planetedge.co.uk>
 
There's more than one way to do it, as they say, here's a lazier way.

while(<*.chr>){
$file = $_;
open(F,$file);
while(<F>){
(
$inval, $server, $name,
$status1, $level, $race,
$class, $zone, $locx,
$locy, $locz
) = split(/\|/,$_);
if ($status1 ne 'Offline'){
# etc
}
close(F);
} Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 

LAZY????

No way, that sweet, I'm already using it Mike,

Tony
 
lazy as in the beneficial trait that makes you a more efficient programmer, not the negative one that prevents you from getting up in the morning. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 

HAhaHa,

That's funny, I needed to laugh today,

I hate mornings, If it wasnt for all the music I look forward to listening to while I work I'd probably sleep through them.

I need to get a signature, that mosquito one is funny.

Tony
 
By the way, how the heck did I get in that top forum expert list thing? People are going to be like, knowing me and stuff, How do you like, get off the list.

Tony
 
Lazy is a good thing, when you're a programmer.

Oh and the list? he he he ..... we've got you now...... Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
I hate to admit it, but, I have a quote hanging in my cube....

&quot;The three great virtures of a programmer: [red]Laziness[/red], Impatience, and Hubris.&quot; -- Larry Wall

Seems we are all following a significant precedent. ;-)


keep the rudder amid ship and beware the odd typo
 

HA,

Yea, I'd say we're doing good then,

I need to test my mail system with someone on Aol, I'm getting reports of people on Aol not receiving outgoing mail from our servers, and I'm using the closest thing to a standard, yahoo, hotmail, & excite are ok with it anyway, attachments and all.

So is anyone on Aol? I'd doubt it, but how about a friend you know or something?

Tony &quot;Java?? Umm, I think that's the stuff I drink while I'm coding perl.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top