• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 136
  • Last Modified:

Opening all ".xml" or specific extension files.

Hi, I need some help urgently.  I want to open ".xml" files using perl, i am able to do this for a single file in the following manner:

open (XML, BIL_plm04_imprint.xml"  ) || die "Cannot not open XML file."; #open file if can't die.

However this method targets a specific XML file, is there another method that opens all ".xml" files?

e.g. *.xml (this doesn't work).

Any thoughts much appreciated.

Thanks

KU
0
KeyurPatel
Asked:
KeyurPatel
  • 3
  • 2
  • 2
  • +2
1 Solution
 
burtdavCommented:
while (<*.xml>) {
  open (XML, $_) || die "Cannot not open XML file.";
  # process XML filehandle
}
0
 
holliCommented:
>> open (XML, BIL_plm04_imprint.xml"  ) || die "Cannot not open XML file."; #open file if can't die.

this is an very interesting point of view.

however burtav is correct, but he does not close the filehandle, what IMHO should be done, at least for clarification.
also using the glob function is a bit more convenient than using the <>-notation for globbing.

eg.
glob ('g:/x/y/z/*') looks nicer than <g:\\x\\y\\z\\*>.

so:
my $file = ""; #obsolete unless "use strict" used

foreach ( glob ('*.xml') )
{
  unless (open XML, $_)
  {
    print "failed to open $_!\n$!\n";
  }
  else
  {
    print "file opened ok $_!\n";
    #your code here
    close XML;
  }
}


regards,
holli
 
0
 
holliCommented:
print reverse ("gnikcuf"), " typos!";

my $file = ""; #obsolete unless "use strict" used

foreach $file ( glob ('*.xml') )
{
 unless (open XML, $file)
 {
   print "failed to open $file!\n$!\n";
 }
 else
 {
   print "file opened ok $file!\n";
   #your code here
   close XML;
 }
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
wilcoxonCommented:
The above answers will work as long as there aren't too many files for perl's globbing mechanism.  This will work regardless of how many files there are.

opendir DIR, '.' or die $!;
while (readdir DIR) {
    next unless /\.xml$/;
    my $file = $_;
    unless (open XML, $file) {
        print "failed to open $file!\n$!\n";
    } else {
        print "file opened ok $file!\n";
        #your code here
        close XML;
    }
}
closedir DIR;
0
 
burtdavCommented:
Can I just defend my answer by saying that the filehandle is automatically closed before it's re-opened? I'd also like to plug wilcoxon's answer above as being optimal... I'd probably just add a check: "close XML or die..."; or else just delete that line for being redundant.
0
 
perlyheadCommented:
This reads in all the files in a directory, but allows you to specify what type of file extensions are allowed.

# Specify valid file extensions here (without the dot).
@validTypes = qw(xml);

# Specify directory of files, preserving *.*
$dir_of_files = "c:\\perl\\files\\*.*";

     foreach $file (glob($dir_of_files))
     {
               foreach $ext (@validTypes)
               {
               $notValid = 0 if ($file =~ /\.$ext$/);          
               }
               # if the file extension is not valid, then skip the file
               next if ($notValid);
          }
0
 
holliCommented:
>> The above answers will work as long as there aren't too many files for perl's globbing mechanism.

Can you pl. explain that a little? i was not aware that there is a limitation.
0
 
KeyurPatelAuthor Commented:
Thank you all for your feedback.  I did start using the glob function Perl offers however I was not aware of its limitations in the number of files it can process hecne I might have to go a few steps back.  At the most I will be processing 300 xml files at a time.  

 
0
 
KeyurPatelAuthor Commented:
Thank you all (Burtday, holli, wilcoxon and perlyhead) for your feedback.  I did start using the glob function Perl offers however I was not aware of its limitations in the number of files it can process hence I might have to go a few steps back.  At the most I will be processing 300 xml files at a time.  The problem lingers on....  

 
0
 
wilcoxonCommented:
[to holli and KeyurPatel]
Perl's globbing mechanism has a limit on the number of files it can handle.  If you only need to process 300 files, globbing should work fine.  The limit as of 5.6.1 or later is in the 1000s (not sure exactly where but I know it fails for 11000 files).
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now