Hi All,

Below script is to print the file content along with folder name where the file sits and it's base folder name. I am able to get the file content and print when ever the file found but problem is everytime it prints first filename and it's folder/basefolder name.

There is main folder name (A2) which has 1 sub folder (MIUY) which inturn has 3 sub folders name (CCT/SST/WMW). All these sub folder has filenames say (CCT023/RETEEYY/WMWMTTY). I was able to capture base folder names and files name but while printing i am not able to pass correct filename/folder name

Can anyone help me out ? File content(will be similar for other files too) :
start putty -ssh -P 2210908A 1009.248090.50904.20001WyhgbvdserMipopA - +l root -pw uyyn.npkju1A
Main script :
#!/usr/bin/perl use warnings; use strict; use File::Find; use Text::CSV; use File::Basename; my $base_dir = 'C:\Test'; # put in your base directory my ($dirname,$filename,$line,$resultfile,@fp,$file); my (@linecolumns,@dir_names,@file,@filenames); find( \&wanted, $base_dir ); sub wanted { return if $_ eq '.' or $_ eq '..'; if (-d) { print " >>> dive into: $_\n" if -d; $dirname = $_; push(@dir_names, basename($_)); } else { #foreach($_){ readout_file($_); ## call subroutine readout_file &write_output;## call subroutine to print std template in outp +ut file print "Output file has been generated successfully.. \n"; #} } }
Sub functions :
#sub function to write input file data into csv format sub readout_file { ($filename) = @_; open my $fh, '<', $filename or die "can't open file:$!"; while (<$fh>) { chomp; s/ /,/g; ## OR s/ /,/g; if you want @linecolumns=split(',',$_); $filename =~ s/.*\///; $filename =~ s/\.[^.]+$//; push(@filenames, $filename); } }
#sub function to write xml format in output file sub write_output{ open ($resultfile, '>>', 'C:\Test\resultfile.dat') or die "Can't open +file: $!"; print $resultfile '<?xml version="1.0" encoding="utf-16"?>'; print $resultfile "\n<!-- **************************************** +************************-->\n"; print $resultfile "<!-- * + *-->\n"; print $resultfile "<!-- * PuTTY Configuration Manager save file - +All right reserved. *-->\n"; print $resultfile "<!-- * + *-->\n"; print $resultfile "<!-- ****************************************** +**********************-->\n"; print $resultfile "<!-- The following lines can be modified at you +r own risks. -->\n"; print $resultfile <<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>$linecolumns[5]</host> <port>$linecolumns[4]</port> <session>Default Settings</session> <commandline>$linecolumns[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 ; print $resultfile "</root>\n"; print $resultfile "</configuration>\n"; #print "Output xml file has been generated successfully.. \n"; }

Output am getting now :

<?xml version="1.0" encoding="utf-16"?> <!-- ****************************************************************- +-> <!-- * *- +-> <!-- * PuTTY Configuration Manager save file - All right reserved. *- +-> <!-- * *- +-> <!-- ****************************************************************- +-> <!-- The following lines can be modified at your own risks. --> <configuration version="0.1.1.2" savepassword="True"> <root type="database" name="A2" expanded="True"> <container type="folder" name="MIUY" expanded="True"> <container type="folder" name="CCT" expanded="True"> <connection type="PuTTY" name="CCT023"> <connection_info> <name>CCT023</name> <protocol>SSH</protocol> <host>1000.24008.50004.20CoioipreA</host> <port>29921</port> <session>Default Settings</session> <commandline>olp.ikmj</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> </root> </configuration> <?xml version="1.0" encoding="utf-16"?> <!-- ****************************************************************- +-> <!-- * *- +-> <!-- * PuTTY Configuration Manager save file - All right reserved. *- +-> <!-- * *- +-> <!-- ****************************************************************- +-> <!-- The following lines can be modified at your own risks. --> <configuration version="0.1.1.2" savepassword="True"> <root type="database" name="A2" expanded="True"> <container type="folder" name="MIUY" expanded="True"> <container type="folder" name="CCT" expanded="True"> <connection type="PuTTY" name="CCT023"> <connection_info> <name>CCT023</name> <protocol>SSH</protocol> <host>10098.298748.509984.200901DukectreirecotA< +/host> <port>278721A</port> <session>Default Settings</session> <commandline>Tuytt.nA</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> </root> </configuration> <?xml version="1.0" encoding="utf-16"?> <!-- ****************************************************************- +-> <!-- * *- +-> <!-- * PuTTY Configuration Manager save file - All right reserved. *- +-> <!-- * *- +-> <!-- ****************************************************************- +-> <!-- The following lines can be modified at your own risks. --> <configuration version="0.1.1.2" savepassword="True"> <root type="database" name="A2" expanded="True"> <container type="folder" name="MIUY" expanded="True"> <container type="folder" name="CCT" expanded="True"> <connection type="PuTTY" name="CCT023"> <connection_info> <name>CCT023</name> <protocol>SSH</protocol> <host>1009.248090.50904.20001WyhgbvdserMipopA</h +ost> <port>2210908A</port> <session>Default Settings</session> <commandline>uyyn.npkju1A</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> </root> </configuration>

output am expecting as

<?xml version="1.0" encoding="utf-16"?> <!-- ****************************************************************- +-> <!-- * *- +-> <!-- * PuTTY Configuration Manager save file - All right reserved. *- +-> <!-- * *- +-> <!-- ****************************************************************- +-> <!-- The following lines can be modified at your own risks. --> <configuration version="0.1.1.2" savepassword="True"> <root type="database" name="A2" expanded="True"> <container type="folder" name="MIUY" expanded="True"> <container type="folder" name="CCT" expanded="True"> <connection type="PuTTY" name="CCT023"> <connection_info> <name>CCT023</name> <protocol>SSH</protocol> <host>1000.24008.50004.20CoioipreA</host> <port>29921</port> <session>Default Settings</session> <commandline>olp.ikmj</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> </root> </configuration> <?xml version="1.0" encoding="utf-16"?> <!-- ****************************************************************- +-> <!-- * *- +-> <!-- * PuTTY Configuration Manager save file - All right reserved. *- +-> <!-- * *- +-> <!-- ****************************************************************- +-> <!-- The following lines can be modified at your own risks. --> <configuration version="0.1.1.2" savepassword="True"> <root type="database" name="A2" expanded="True"> <container type="folder" name="MIUY" expanded="True"> <container type="folder" name="SST" expanded="True"> <connection type="PuTTY" name="RETEEYY"> <connection_info> <name>RETEEYY</name> <protocol>SSH</protocol> <host>10098.298748.509984.200901DukectreirecotA< +/host> <port>278721A</port> <session>Default Settings</session> <commandline>Tuytt.nA</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> </root> </configuration> <?xml version="1.0" encoding="utf-16"?> <!-- ****************************************************************- +-> <!-- * *- +-> <!-- * PuTTY Configuration Manager save file - All right reserved. *- +-> <!-- * *- +-> <!-- ****************************************************************- +-> <!-- The following lines can be modified at your own risks. --> <configuration version="0.1.1.2" savepassword="True"> <root type="database" name="A2" expanded="True"> <container type="folder" name="MIUY" expanded="True"> <container type="folder" name="WMW" expanded="True"> <connection type="PuTTY" name="WMWMTTY"> <connection_info> <name>WMWMTTY</name> <protocol>SSH</protocol> <host>1009.248090.50904.20001WyhgbvdserMipopA</h +ost> <port>2210908A</port> <session>Default Settings</session> <commandline>uyyn.npkju1A</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> </root> </configuration>

In reply to looping logic error while printing output from folder/flie content by Vijay81

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.