Problem writing/wrapping files under folder using perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem writing/wrapping files under folder using perl
# 1  
Old 01-16-2013
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 file in sub directory, then in xml format, these file information should come under sub directory name but current script
write all the file information under only main sub directory level2.
Ex :
Path : C:\SD\Setup\Data
In the above path, there will sub dir level1 : UAINI
C:\SD\Setup\Data\UAINI\
under this level1, there will another level2 of sub dir : Base
C:\SD\Setup\Data\UAINI\Base
under this level2, there will 3 sub dir's level3 :
Code:
A1 : 1-A.bat,1-AD.bat files
B2 : 2-BDD.bat, 2-BEE.bat files
W1 : 1-WM.bat, 2-WMA.bat files

each of these sub dir's will have 1 or more files
file content will be:
Code:
start putty -ssh -P 22 10.24.04.20A -Q root -pw olp.ikmj

now after running the main script,i get the xml format with the content under Base dir as container(level2). Instead, should get xml format
with having 3 containers and each container having sub dir's level3 name and it's file content.
Code:
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
my $basedir = "C:/SD/Setup/Data/";
my $envname = "HMDS_EnvSetup";
my $counter;
my @basedir;
open( my $resultfile, '>', 'C:\SD\Setup\Results\resultfile.dat' ) or die "resultfile.dat: $!";
# the next bit should only be written one time per xml output file:
print $resultfile <<EOH;
<?xml version="1.0" encoding="UTF-8"?>
<!-- *****************************************-->
<!-- *                                       *-->
<!-- * 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="$envname" expanded="True">
EOH
my @containers;
my $indent = '';
#print scalar(@containers);
find( \&wanted, $basedir );
if ( @containers ) {
    for ( 1 .. $#containers ) {
        print $resultfile "$indent</container>\n";
        chop $indent;
    }
}
print $resultfile "</root>\n</configuration>\n";
sub wanted {
    return if ( /^..?$/ );
    if ( -d ) {
        my ( $root, @levels ) = split m{/}, $File::Find::dir;
  print "levels are :@levels\n";
  print "Containers are : @containers\n";
        if ( @levels >= @containers ) {  # push one level deeper
            $indent .= " ";
            print $resultfile "$indent<container type=\"folder\" name=\"$_\" expanded=\"True\">\n";
   $counter++;
        }
        elsif ( @levels <= @containers ) {  # pop out one level
            print $resultfile "$indent</container>\n";
            chop $indent;
        }
        elsif ( $levels[-1] ne $containers[-1] ) {  # same level, different path
            print $resultfile "$indent</container>\n";
            print $resultfile "$indent<container type=\"folder\" name=\"$levels[-1]\" expanded=\"True\">\n";
        }
        @containers = @levels;
    }
    elsif ( -f _ and -s _ ) { 
        process_file();
    }
}
sub process_file {
    my $filename = $_;
    open my $fh, '<', $filename or die "$File::Find::name : $!\n";
    my @linecolumns;
    while (<$fh>) {
        chomp;
  s/ /,/g;
        @linecolumns=split(',',$_);
    }
    print $resultfile <<ETX
$indent<connection type="PuTTY" name="$filename">
$indent <connection_info>
$indent  <name>$filename</name>
$indent  <protocol>SSH</protocol>
$indent  <host>$linecolumns[5]</host>
$indent  <port>$linecolumns[4]</port>
$indent  <session>Default Settings</session>
$indent  <commandline>$linecolumns[6] $linecolumns[7] $linecolumns[8] $linecolumns[9]</commandline>
$indent  <description />                  
$indent </connection_info>
$indent <login>
$indent <login />
$indent  <password />
$indent  <prompt />
$indent </login>
$indent</connection>
ETX
;
}

now getting output as :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- *****************************************-->
<!-- *                                       *-->
<!-- * 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="EnvSetup" expanded="True">
 <container type="folder" name="UAINI" expanded="True">
  <container type="folder" name="Base" expanded="True">
  <connection type="PuTTY" name="1-WM.bat">
   <connection_info>
    <name>1-WM.bat</name>
    <protocol>SSH</protocol>
    <host>19.20.54.21</host>
    <port>221</port>
    <session>Default Settings</session>
    <commandline>-l root -pw uy.ju</commandline>
    <description />                  
   </connection_info>
   <login>
   <login />
    <password />
    <prompt />
   </login>
  </connection>
  <connection type="PuTTY" name="2-WMA.bat">
   <connection_info>
    <name>2-WMA.bat</name>
    <protocol>SSH</protocol>
    <host>19.20.54.21</host>
    <port>221</port>
    <session>Default Settings</session>
    <commandline>-l root -pw uy.ju</commandline>
    <description />                  
   </connection_info>
   <login>
   <login />
    <password />
    <prompt />
   </login>
  </connection>
  <connection type="PuTTY" name="2-BBD.bat">
   <connection_info>
    <name>2-BBD.bat</name>
    <protocol>SSH</protocol>
    <host>98.28.54.21</host>
    <port>27</port>
    <session>Default Settings</session>
    <commandline>-l root -pw direct.nA</commandline>
    <description />                  
   </connection_info>
   <login>
   <login />
    <password />
    <prompt />
   </login>
  </connection>
  <connection type="PuTTY" name="2-BBE.bat">
   <connection_info>
    <name>2-BBE.bat</name>
    <protocol>SSH</protocol>
    <host>18.38.58.88</host>
    <port>28</port>
    <session>Default Settings</session>
    <commandline>-l root -pw direct.2A</commandline>
    <description />                  
   </connection_info>
   <login>
   <login />
    <password />
    <prompt />
   </login>
  </connection>
  <connection type="PuTTY" name="1-A.bat">
   <connection_info>
    <name>1-A.bat</name>
    <protocol>SSH</protocol>
    <host>10.24.04.20A</host>
    <port>22</port>
    <session>Default Settings</session>
    <commandline>-l root -pw olp.ikmj</commandline>
    <description />                  
   </connection_info>
   <login>
   <login />
    <password />
    <prompt />
   </login>
  </connection>
  <connection type="PuTTY" name="1-AD.bat">
   <connection_info>
    <name>1-AD.bat</name>
    <protocol>SSH</protocol>
    <host>09.25.05.10B</host>
    <port>11</port>
    <session>Default Settings</session>
    <commandline>-l root -pw llb.ujyh</commandline>
    <description />                  
   </connection_info>
   <login>
   <login />
    <password />
    <prompt />
   </login>
  </connection>
  </container>
 </container>
</root>
</configuration>


Attached png file to show how current output looks like.


Instead am trying to get like this : please note i added container tag manually here to get each sub dir's level3 dir name/wrapper.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- *****************************************-->
<!-- *                                       *-->
<!-- * 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="EnvSetup" expanded="True">
 <container type="folder" name="UAINI" expanded="True">
  <container type="folder" name="Base" expanded="True">
 
  <container type="folder" name="W1" expanded="True">#-----This added manually
 
  <connection type="PuTTY" name="1-WM.bat">
   <connection_info>
    <name>1-WM.bat</name>
    <protocol>SSH</protocol>
    <host>19.20.54.21</host>
    <port>221</port>
    <session>Default Settings</session>
    <commandline>-l root -pw uy.ju</commandline>
    <description />                  
   </connection_info>
   <login>
   <login />
    <password />
    <prompt />
   </login>
  </connection>
  <connection type="PuTTY" name="2-WMA.bat">
   <connection_info>
    <name>2-WMA.bat</name>
    <protocol>SSH</protocol>
    <host>19.20.54.21</host>
    <port>221</port>
    <session>Default Settings</session>
    <commandline>-l root -pw uy.ju</commandline>
    <description />                  
   </connection_info>
   <login>
   <login />
    <password />
    <prompt />
   </login>
  </connection>
 
  </container>#-----This added manually
 
 
 
  <container type="folder" name="B2" expanded="True">#-----This added manually
 
  <connection type="PuTTY" name="2-BBD.bat">
   <connection_info>
    <name>2-BBD.bat</name>
    <protocol>SSH</protocol>
    <host>98.28.54.21</host>
    <port>27</port>
    <session>Default Settings</session>
    <commandline>-l root -pw direct.nA</commandline>
    <description />                  
   </connection_info>
   <login>
   <login />
    <password />
    <prompt />
   </login>
  </connection>
  <connection type="PuTTY" name="2-BBE.bat">
   <connection_info>
    <name>2-BBE.bat</name>
    <protocol>SSH</protocol>
    <host>18.38.58.88</host>
    <port>28</port>
    <session>Default Settings</session>
    <commandline>-l root -pw direct.2A</commandline>
    <description />                  
   </connection_info>
   <login>
   <login />
    <password />
    <prompt />
   </login>
  </connection>
 
  </container>#-----This added manually
 
 
 
  <container type="folder" name="A1" expanded="True"> #-----This added manually
 
  <connection type="PuTTY" name="1-A.bat">
   <connection_info>
    <name>1-A.bat</name>
    <protocol>SSH</protocol>
    <host>10.24.04.20A</host>
    <port>22</port>
    <session>Default Settings</session>
    <commandline>-l root -pw olp.ikmj</commandline>
    <description />                  
   </connection_info>
   <login>
   <login />
    <password />
    <prompt />
   </login>
  </connection>
  <connection type="PuTTY" name="1-AD.bat">
   <connection_info>
    <name>1-AD.bat</name>
    <protocol>SSH</protocol>
    <host>09.25.05.10B</host>
    <port>11</port>
    <session>Default Settings</session>
    <commandline>-l root -pw llb.ujyh</commandline>
    <description />                  
   </connection_info>
   <login>
   <login />
    <password />
    <prompt />
   </login>
  </connection>
 
  </container> #-----This added manually
 
  </container>
 </container>
</root>
</configuration>

Problem writing/wrapping files under folder using perl-envsetppng

Last edited by jim mcnamara; 01-16-2013 at 08:01 PM..
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

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

3. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: cowboymaverick
3 Replies

4. 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

5. Shell Programming and Scripting

Problem in copy files to a folder

Hi all, cp -r /tmp/Agent/* /apps/opt/Agent/TEST When I copy files under /tmp/Agent using this command Files are getting copied to Agent folder also but I need only in the TEST folder. Is there any way to fix this issue??\ Thanks in advance Ananth (4 Replies)
Discussion started by: Ananthdoss
4 Replies

6. Shell Programming and Scripting

Writing a Perl Script that processes multiple files

I want to write a Perl script that manipulates multiple files. In the directory, I have files 250.*chr$.ped where * is from 1 to 1000 and $ is from 1-22 for a total of 22 x 10,000 = 22,000 files. I want to write a script that only manipulates files 250.1chr*.ped where * is from 1 to 22.... (10 Replies)
Discussion started by: evelibertine
10 Replies

7. Solaris

Space problem writing to Solaris Folder

Hello, I've got a process failure which says it may be caused by insufficient space in the directory. Is there a way I can tell what the maximum allowable size is? I've done df -k and there are no file systems over 45% so if it is a space problem it's confined to the sub directory. (4 Replies)
Discussion started by: Grueben
4 Replies

8. Shell Programming and Scripting

Problem writing to different files

Hello: I have the following code: ---------------------------------- open (OUTPUT_FILE, ">>/usr/users/rovolis/PREPAID/CC/TCG/PP.$cyear$cmonth$cday.txt")||die "$!"; 82 open (OUTPUT_FILE2, ">>/usr/users/rovolis/PREPAID/CC/TCG/PR.$cyear$cmonth$cday.txt")||die "$!"; 83 # ... (0 Replies)
Discussion started by: chriss_58
0 Replies

9. UNIX for Advanced & Expert Users

Auto copy for files from folder to folder upon instant writing

Hello all, I'm trying to accomplish that if a file gets written to folder /path/to/a/ it gets automatically copied into /path/to/b/ the moment its get written. I thought of writing a shell script and cron it that every X amount of minutes it copies these files over but this will not help me... (2 Replies)
Discussion started by: Bashar
2 Replies

10. Shell Programming and Scripting

Line wrapping problem when using awk

I am fairly new at this, I wrote a awk program to give me some summary information about a file. At the end of the program I want to print some variables but for some reason it keeps wrapping the last variable on a new line in the output file. Here is the print command print "99", file_id,... (1 Reply)
Discussion started by: placroix1
1 Replies
Login or Register to Ask a Question