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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

opening all text files in a directory 2

Status
Not open for further replies.

perlone

Programmer
Joined
May 20, 2001
Messages
438
Location
US
Hi,

I have a directory that contains text files named like the following:

1.txt
2.txt
3.txt
4.txt
5.txt
and so on...

All these files are located "$root/cgi-bin/trading" directory. How do I open all those text files? Thanks for your time. -Aaron
 
Here:

You would open the directory, read through all the files, then open each one at a time like so:

[tt]
opendir(DIR,"$root/cgi-bin/trading") || die("Failed to open dir: $!");
while ($file = readdir(DIR)) {
if (($file eq ".") || ($file eq "..")) {
# do nothing
} else {
open(FILE,">>$root/cgi-bin/trading/$file") || die("Failed to open file: $!");
@data = <FILE>;
# do what you want to do with the file
close(FILE);
}
}
closedir(DIR);
[/tt]

Thats how I would do it.

Hope this helps.

-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
or the lazy way :-)

while(<*.txt>){
[tab]open(F,$_) || warn &quot;Can't open $_\n$!\n&quot;;
[tab]# do something with file
[tab]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.
 
Good answer Mike! You don't see perl automatic file-globbing used very often. I think most of us tend to forget that it even exists!
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top