?
Solved

Opening all ".xml" or specific extension files.

Posted on 2003-04-01
10
Medium Priority
?
135 Views
Last Modified: 2013-11-19
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
Comment
Question by:KeyurPatel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 5

Expert Comment

by:burtdav
ID: 8244882
while (<*.xml>) {
  open (XML, $_) || die "Cannot not open XML file.";
  # process XML filehandle
}
0
 
LVL 6

Expert Comment

by:holli
ID: 8247143
>> 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
 
LVL 6

Expert Comment

by:holli
ID: 8247161
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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
LVL 26

Accepted Solution

by:
wilcoxon earned 200 total points
ID: 8248167
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
 
LVL 5

Expert Comment

by:burtdav
ID: 8248951
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
 

Expert Comment

by:perlyhead
ID: 8249556
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
 
LVL 6

Expert Comment

by:holli
ID: 8251821
>> 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
 

Author Comment

by:KeyurPatel
ID: 8252660
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
 

Author Comment

by:KeyurPatel
ID: 8252664
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
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8254713
[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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question