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

Getting Information from files

Status
Not open for further replies.

biobrain

MIS
Joined
Jun 21, 2007
Messages
90
Location
GB
Dear All,

I want to write a perl script which can parse (retrieve) specific information from all the files in a folder with a particular extension.

i.e in a Folder I have files with the following extension .xyz

Now i wish to write a script which could parse all the files in this folder.

For a Single File I can easily do this by a script like this one.

But I want to automate this so that It can read every file with a particular extension in my folder.


# record user input and store in a variable using

$myanswer=<STDIN>;

# defined a new veriable for file name
$myfilename=$myanswer ;

# open the file if available in the directory otherwise die

open (SP, "$myfilename") or
die "Your desired file $myfilename is not found in the directory, please correct if this is a typo, or search an other directory\n";

# open the new output file to be saved in HTML format
open(OUTPUT,">output.txt");

while (<SP>){
if ($_=~/sometext/ )
{

printf(OUTPUT " <li> $1 </li>");
print (OUTPUT "<br>\n");
}
}


# close the open file handels
close SP;
close OUTPUT;



Please reply me as early as possible

Regards
 
use a glob to get a list of all .xyz files:

Code:
my @all_xyz_files = <path/to/files/*.xyz>;

or, as travs69 recommends, use opendir() and grep:

Code:
opendir(DIR,"path/to/files") or die "$!";
my @all_xyz_files  = grep {/\.xyz$/} readdir DIR;
close DIR;

now process all the files in the @all_xyz_files array.






------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Dear KevinADC and travs69

Thanks for your reply. I will try these instruction now.

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top