Tag Archives: BBE

Problem writing/wrapping files under folder using perl

By Optimus81

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:SDSetupData
In the above path, there will sub dir level1 : UAINI
C:SDSetupDataUAINI
under this level1, there will another level2 of sub dir : Base
C:SDSetupDataUAINIBase
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:SDSetupResultsresultfile.dat' ) or die "resultfile.dat: $!";
# the next bit should only be written one time per xml output file:
print $resultfile <<EOH;






EOH
my @containers;
my $indent = '';
#print scalar(@containers);
find( &wanted, $basedir );
if ( @containers ) {
for ( 1 .. $#containers ) {
print $resultfile "$indentn";
chop $indent;
}
}
print $resultfile "nn";
sub wanted {
return if ( /^..?$/ );
if ( -d ) {
my ( $root, @levels ) = split m{/}, $File::Find::dir;
print "levels are :@levelsn";
print "Containers are : @containersn";
if ( @levels >= @containers ) { # push one level deeper
$indent .= " ";
print $resultfile "$indentn";
$counter++;
}
elsif ( @levels <= @containers ) { # pop out one level
print $resultfile "$indentn";
chop $indent;
}
elsif ( $levels[-1] ne $containers[-1] ) { # same level, different path
print $resultfile "$indentn";
print $resultfile "$indentn";
}
@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 () {
chomp;
s/ /,/g;
@linecolumns=split(',',$_);
}
print $resultfile <<ETX
$indent
$indent
$indent $filename
$indent SSH
$indent $linecolumns[5]
$indent $linecolumns[4]
$indent Default Settings
$indent $linecolumns[6] $linecolumns[7] $linecolumns[8] $linecolumns[9]
$indent
$indent
$indent
$indent
$indent
$indent
$indent
$indent
ETX
;
}


now getting output as :

Code:







1-WM.bat
SSH
19.20.54.21
221
Default Settings
-l root -pw uy.ju

2-WMA.bat
SSH
19.20.54.21
221
Default Settings
-l root -pw uy.ju

2-BBD.bat
SSH
98.28.54.21
27
Default Settings
-l root -pw direct.nA

2-BBE.bat
SSH
18.38.58.88
28
Default Settings
-l root -pw direct.2A

1-A.bat
SSH
10.24.04.20A
22
Default Settings
-l root -pw olp.ikmj

1-AD.bat
SSH
09.25.05.10B
11
Default Settings
-l root -pw llb.ujyh


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:







#-----This added manually

1-WM.bat
SSH
19.20.54.21
221
Default Settings
-l root -pw uy.ju

2-WMA.bat
SSH
19.20.54.21
221
Default Settings
-l root -pw uy.ju

#-----This added manually

#-----This added manually

2-BBD.bat
SSH
98.28.54.21
27
Default Settings
-l root -pw direct.nA

2-BBE.bat
SSH
18.38.58.88
28
Default Settings
-l root -pw direct.2A

#-----This added manually

#-----This added manually

1-A.bat
SSH
10.24.04.20A
22
Default Settings
-l root -pw olp.ikmj

1-AD.bat
SSH
09.25.05.10B
11
Default Settings
-l root -pw llb.ujyh

#-----This added manually


Source: FULL ARTICLE at The UNIX and Linux Forums