Perl script to parse all files in the folder

Hello Smart People!

I have a perl script that will import xml data into an access db.

I would like to modify it so it will automatcially parse through all xml files in the folder. I swa a post but couldnt get it working. her is what my scrip looks like, i only list the top if you need more let me know.

use strict;
use XML::DOM;
use DBI;
use Data::GUID;
#usage: xml.pl
my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile ($ARGV[0]);

I really don't care if i use a seperate script that calls the import based on files in the folder. But it would be nicer all in one. Thanks for any ideas you might have, I appreciate it!

What you can do is to 'steal' some of the boiler plate from 'find2perl'. Here is an example of recursively finding all the xml files and print out the content.

$ find2perl /somedir -name "*.xml"
#! /usr/bin/perl -w
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
        if 0; #$running_under_some_shell

use strict;
use File::Find ();

# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

sub wanted;



# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '/somedir');
exit;


sub wanted {
    /^.*\.xml\z/s
    && print("$name\n");
}

Simply include your code in the 'sub wanted'. Hope this helps.

Hi,

Try this user defined function in your code,

my $xmlpat=qr/\.xml$/;
my $xmlfiles=&getFiles("/home/dir",$xmlpat);

sub getFiles() {
  my $dir=shift;
  my $pattern=shift;

  open(DIR,"$dir");
  my @files=grep{/$pattern/} readdir(DIR);
  closedir(DIR);
  return(@files);
}

I hope it helps you.
Cheers,
Ranga:-)

Thanks soo much. Let me try it out , I'll let you know how it goes! Thanks!