perl - Processing All Files in a Directory -
i'm new here , need help, i'm not programmer debrouille more in development of web pages (java). malheuereusement have not been able find internship position in field, have found in each other area having no other choice, can accept that, unfortunately training master not programmer, director of system , made him discover thing in perl. come in on problem: able implement perl-script parses file-xml, must go editor perl write names of files, unfortunately have thousands of files, xml, , ' love code has able find all-xml files located in folder , following naturement parser without time began write editor name of thousands of files. that's problem. if possessed in 4 or 6 files, have written names in editor-perl , have received result in immediate
i can somehow summarizes thus:
1- (extracts xml perl-script) saved in folder on pc stuff 2- perl-script searches xml files in folder 3- perl-script processes (parse) fichiers_xml 4- perl-script returns me result of parsing text file
here code paser für implements, works well
use strict; use warnings; use xml::twig; use file::find; $file1 = shift or die 'no file1'; @files = @argv or die 'no files'; $fileresult = 'result.csv'; open( $fhresult, '>', $fileresult )or die ("unable open file $fileresult\n$!"); $twig1= xml::twig->new( twig_handlers => { 'parameter' => sub { $attr_name = $_->{'att'}->{'name'} // 'fault'; print $fhresult $attr_name . ","; }, }, ); print $fhresult( (split('_', $file1,2))[0] . ',' ); $twig1->parsefile($file1); $file (@files) { $twig2 = xml::twig->new( twig_handlers => { 'parameter' => sub { $attr_value = $_->{'att'}->{'value'} // 'fault'; print $fhresult $attr_value . ","; }, }, ); print $fhresult ( split( '_', "\n$file", 2 ) )[0] . ','; $twig2->parsefile($file); } close $fhresult;
if understand question correctly, want way find of xml files in directory.
here's 1 way:
chdir $the_directory_we_want_to_process; foreach $xml_file ( glob('*.xml') ) { process_the_file($xml_file); } the glob('*.xml') function performs shell pattern match generate list of names of files in current directory ending in '.xml'
here's another:
opendir(dh, $the_directory_we_want_to_process) while ( $file = readdir(dh) ) { next unless /\.xml$/; process_the_file($xml_file); } closedir(dh);
Comments
Post a Comment