sdruss
asked on
Ned Perl Snippet to Read Files in Directory
Need perl snippet to process files in directory. Ultimately need to truncate database tables and reload with initial seed data. Table names are determined from file name, such that: <table_name.xml>. Hence:
1) need to read all files from directory that have a ".xml" file extension
2) strip each file name of file extension to get database table name
3) generate SQL to "truncate table <table_name.xml>" for each file name
1) need to read all files from directory that have a ".xml" file extension
2) strip each file name of file extension to get database table name
3) generate SQL to "truncate table <table_name.xml>" for each file name
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, Unix. Will attempt Wilcoxon's solution today. Thanks!
So you're not after a one-liner like the one I provided then, sdruss? You want something to embed as part of an existing Perl script, right?
ASKER
Worked, extremely, extremely well! Thank you.
And when you say:
generate SQL to "truncate table <table_name.xml>" for each file name
Do you mean:
generate SQL to "truncate table <table_name>" for each file name
?
Here's a possibility for UNIX/Linux:
Assuming we have these files:
table1.xml
table2.xml
This command:
ls *.xml | perl -ne 'print "truncate table $1;\n" if /(.+)\.xml$/'
should give this output:
truncate table table1;
truncate table table2;
Of course the "ls *.xml" part is not Perl, and it sounds as if you probably want this to be part of an existing script, right? If so, perhaps wilcoxon's solution would be what you want.