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";
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.