Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Recursive file processing from a path and printing output in a file

by Vijay81 (Acolyte)
on Dec 09, 2012 at 21:14 UTC ( #1008003=note: print w/ replies, xml ) Need Help??


in reply to Recursive file processing from a path and printing output in a file

Thanks guys for you inputs.

I now able to read directories and sub directoires and able to print the xml output file.

made some cleanup work on my inital script so it will be much more eaiser to read and follow.

problem now is it's not recusively going through sub-directoires and printing th xml output. It just reads first sub-directories file1 content and print just that as output

Can anyone tell me which loop i am missing out

Script:
#! /usr/bin/perl use strict; use warnings; use File::Basename; use File::Find; use Class::CSV; use Text::CSV; use XML::TreeBuilder; use Data::Dumper; #variable/arrays declartions my ($csv,$inputfilename,$resultfile,$fh,$line,$dir,$fp,$base_dir,$dh,$ +file,$mycontainerless,$paramsfile,$connection,$resultxmlfile,$config) +; my ($tree,$container,$row,$contree,$index,$tag,$tagname); my (@dir,@dir_names,@filenames,@taglist,@columns); #path declartions $base_dir = 'C:\Test'; $paramsfile = 'C:\Test\input.csv'; @dir = $base_dir; #read through the given directory path to look for dir/subdir's while (@dir) { $dir = pop(@dir); opendir($dh, $dir); while($file = readdir($dh)) { next if $file eq '.'; next if $file eq '..'; $file = "$dir/$file"; if (-d $file) { push(@dir, $file); push(@dir_names, basename($file)); } elsif (-f $file) { $file =~ s/.*\///; $file =~ s/\.[^.]+$//; push(@filenames, $file); } } } &write_datato_csvfile; #sleep(25); print "<!-- ************************************************** +**************-->\n"; print @dir_names; print "\n"; print "<!-- ************************************************** +**************-->\n"; print @filenames; print "\n"; print "<!-- ************************************************** +**************-->\n"; &write_output_xml; # sub functions #sub function to write input file data into csv format sub write_datato_csvfile{ open $resultfile, '>>', 'C:\Test\input.csv' or die "Can't open file: $ +!"; foreach $fp (glob("$dir/*.bat")) { print $fp; print "\n"; open ($fh, "<", $fp) or die "can't read open '$fp':"; while ($line = <$fh>) { $line=~ s/ /,/g; print $resultfile $line; print $resultfile "\n"; } close $fh or die "can't read close '$fp':"; } #close $fh or die "can't read close '$fp':"; } #sub function to write xml format in output file sub write_output_xml{ open ($resultxmlfile, '>>', 'C:\Test\resultxml.xml') or die "Can't ope +n file: $!"; print $resultxmlfile '<?xml version="1.0" encoding="utf-16"?>'; print $resultxmlfile "\n<!-- ************************************* +***************************-->\n"; print $resultxmlfile "<!-- * + *-->\n"; print $resultxmlfile "<!-- * PuTTY Configuration Manager save file + - All right reserved. *-->\n"; print $resultxmlfile "<!-- * + *-->\n"; print $resultxmlfile "<!-- *************************************** +*************************-->\n"; print $resultxmlfile "<!-- The following lines can be modified at +your own risks. -->\n"; #&write_datato_csvfile; #sleep(30); #my $time = 15; #while($time--){ #sleep(1); #} $resultfile = 'C:\Test\input.csv'; my $csv = Text::CSV->new(); open (CSV, "<", $resultfile) or die $!; print "Reading input csv file.... \n"; while (<CSV>) { next if ($. == 0); if ($csv->parse($_)) { @columns = $csv->fields(); #print "$columns[1] \n"; } else { my $err = $csv->error_input; print "Failed to parse line: $err"; } print $resultxmlfile <<EOF <configuration version="0.1.1.2" savepassword="True"> <root type="database" name="$dir_names[0]" expanded="True"> <container type="folder" name="$dir_names[1]" expanded="True"> <container type="folder" name="$dir_names[2]" expanded="Tr +ue"> <connection type="PuTTY" name="$filenames[0]"> <connection_info> <name>$filenames[0]</name> <protocol>SSH</protocol> <host>$columns[5]</host> <port>$columns[4]</port> <session>Default Settings</session> <commandline>$columns[9]</commandline> <description /> </connection_info> <login> </login> <password /> <prompt /> </login> <timeout> <connectiontimeout>1000</connectiontimeout> <logintimeout>750</logintimeout> <passwordtimeout>750</passwordtimeout> <commandtimeout>750</commandtimeout> </timeout> <command> <command1 /> <command2 /> <command3 /> <command4 /> <command5 /> </command> <options> <loginmacro>False</loginmacro> <postcommands>False</postcommands> <endlinechar>10</endlinechar> </options> </connection> EOF ; } close CSV; print $resultxmlfile "</root>\n"; print $resultxmlfile "</configuration>\n"; #close CSV; print "Output xml file has been generated successfully.. \n"; }


Comment on Re: Recursive file processing from a path and printing output in a file
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1008003]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2014-07-13 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (248 votes), past polls