Perl script to parse all files in the folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script to parse all files in the folder
# 1  
Old 07-29-2012
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.

Code:
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!

Last edited by Scott; 07-29-2012 at 06:02 PM.. Reason: Please use code tags
# 2  
Old 07-29-2012
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.

Code:
$ 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.
# 3  
Old 07-30-2012
perl

Hi,

Try this user defined function in your code,
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:-)
# 4  
Old 07-30-2012
Thanks soo much. Let me try it out , I'll let you know how it goes! Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

2. Shell Programming and Scripting

Problem writing/wrapping files under folder using perl

I have a script which generates env setup xml file by reading path.It read the path and checks if there is any file/dir present recurseively. If a file is found under sub directory then it will read the file and the values from the file are passed to generate xml format. Problem is if i have a... (0 Replies)
Discussion started by: Optimus81
0 Replies

3. Shell Programming and Scripting

Perl script to parse multiple windows event logs.

Hi all, I am developing a log parsing agent in perl to send windows Event logs to Zenoss Monitoring tool. Using Win32::EventLog i can able to get the Event messages but only one Eventype eg Application or System could able to parse at a time. Can you please help to how to open mutiple eventlogs... (3 Replies)
Discussion started by: kar_333
3 Replies

4. Programming

CGI Perl script to execute bash script- unable to create folder

Hi I have a bash script which takes parameters sh /tmp/gdg.sh -b BASE-NAME -n 1 -s /source/data -p /dest/data/archive -m ARC gdg.sh will scan the /source/data and will move the contents to /dest/data/archive after passing through some filters. Its working superb from bash I have... (0 Replies)
Discussion started by: rakeshkumar
0 Replies

5. Shell Programming and Scripting

Pick files after specified Time from a folder in Perl

Hi All I am stuck in a problem and wants help What i want to do is I have a directory(say name of that directory is outdir) In that directory(outdir) the files come after some gap of time by some processes. What i want to do is i want to list all the files in that directory after the given... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

6. Shell Programming and Scripting

awk script to parse results from TWO files

I am trying to parse two files and get data that does not match in one of the columns ( column 3 in my case ) Data for two files are as follows A.txt ===== abc 10 5 0 1 16 xyz 16 1 1 0 18 efg 30 8 0 2 40 ijk 22 2 0 1 25 B.txt ===== abc... (6 Replies)
Discussion started by: roger67
6 Replies

7. Shell Programming and Scripting

Perl script to parse output and print it comma separated

I need to arrange output of SQL query into a comma separated format and I'm struggling with processing the output... The output is something like this: <Attribute1 name><x amount of white spaces><Atribute value> <Attribute2 name><x amount of white spaces><Atribute value> <Attribute3... (2 Replies)
Discussion started by: Juha
2 Replies

8. Shell Programming and Scripting

Shell script (not Perl) to parse xml with awk

Hi, I have to make an script according to these: - I have couples of files like: xxxxxxxxxxxxx.csv xxxxxxxxxxxxx_desc.xml - every xml file has diferent fields, but keeps this format: ........ <defaultName>2011-02-25T16:43:43.582Z</defaultName> ........... (2 Replies)
Discussion started by: Pluff
2 Replies

9. Shell Programming and Scripting

script for Finding files in a folder and copying to another folder

Hi all, I have a folder '/samplefolder' in which i have some files like data0.txt, data1.txt and data2.txt. I have to search the folder for existence of the file data0.txt first and if found have to copy it to some other file; next i have to search the folder for existence of file... (5 Replies)
Discussion started by: satish2712
5 Replies

10. Shell Programming and Scripting

Parse the .txt file for folder name and FTP to the corrsponding folder.

Oracle procedure create files on UNIX folder on a regular basis. I need to FTP files onto windows server and place the files, based on their name, in the corresponding folders. File name is as follows: ccyymmddfoldernamefile.txt; Folder Name length could be of any size; however, the prefix and... (3 Replies)
Discussion started by: MeganP
3 Replies
Login or Register to Ask a Question