#!/usr/bin/perl use warnings; use strict; use File::Find; use File::Basename; my $base_dir = 'C:\Testdata'; # put in your base directory my ($dirname,$filename,$line,$resultfile,@fp,$file,$basedir1,$subdirlevel1,$subdirlevel2); my (@linecolumns,@dir_names,@file,@filenames,@filecount); open ($resultfile, '>>', 'C:\Results\resultfile.txt') or die "Can't open file: $!"; print $resultfile ''; print $resultfile "\n\n"; print $resultfile "\n"; print $resultfile "\n"; print $resultfile "\n"; print $resultfile "\n"; print $resultfile "\n"; opendir(IMD, $base_dir) || die("Cannot open directory"); my @thefiles= readdir(IMD); next if @thefiles eq '.' or @thefiles eq '..'; push(@thefiles, basename($base_dir)); #print $thefiles[2]; closedir(IMD); print $resultfile < TTX ; find( \&wanted, $base_dir ); print $resultfile "\n"; print $resultfile "\n"; sub wanted { return if $_ eq '.' or $_ eq '..'; if (-d) { print " >>> dive into: $_\n" if -d; $dirname = $_; push(@dir_names, basename($_)); } else { $basedir1 = $dir_names[0]; readout_file($_); ## call subroutine readout_file &write_output($_);## call subroutine to print std template in output file print "Output file has been generated successfully.. \n"; } } #### #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); } foreach(@dir_names){ (my $name,my $dir,my $ext) = fileparse($dir_names[1],'\..*'); $subdirlevel1 = $name; $filename =~ s/.*\///; $filename =~ s/\.[^.]+$//; $subdirlevel2=(basename($filename)); print $dir_names[3], $/; } } #sub function to write xml format in output file sub write_output{ print $resultfile < $filename SSH $linecolumns[5] $linecolumns[4] Default Settings $linecolumns[10] 1000 750 750 750 False False 10 EOF ; } #### ### first time getting correct subdirlevel2 name ######## need to get foldername CCT not filename CCT023 SSH 1000.24008.50004.20CoioipreA 29921 Default Settings 1000 750 750 750 False False 10 ###### No need to print this line again ######### Need to get correct subdirlevel name which SST RETEEYY SSH 10098.298748.509984.200901DukectreirecotA 278721A Default Settings 1000 750 750 750 False False 10 ####################### not required ###### No need to print this line again ######### Need to get correct subdirlevel name which WMW WMWMTTY SSH 1009.248090.50904.20001WyhgbvdserMipopA 2210908A Default Settings 1000 750 750 750 False False 10 ########################## Not need