In details : path give say : "C:\test" Dir1 -- inside Dir1 there is one more sub directories say Dir2 Dir2 -- inside Dir2 there 2 more sub directories say Dir2a/Dir2b (Each of the sub directories Dir2a/b has 2 files each) Dir3 -- inside Dir3 there is 2 more sub directories say Dir3a (Each of the sub directories Dir3a has 2 files each) #### #! /usr/bin/perl use strict; use warnings; use File::Basename; use File::Find; use Data::Dumper; use Class::CSV; use Text::CSV; use XML::TreeBuilder; use Data::Dumper; my ($csv,$inputfilename,$resultfile,$fh,$line,$dir,$fp,$base_dir,$dh,$file); my (@dir,@dir_names); $base_dir = 'C:\test'; @dir = $base_dir; # logic to travel through path and gets the directory names(just name) 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)); } else { } } } #print scalar @dir_names; my $dir_count = scalar @dir_names; print "The no of folder persent in the path $dir_count"; print "\n"; #logic to travel through given same path and gets the file names (just name of the file) print "\n"; print "\n"; my $dirfile = 'C:\Test'; find({ wanted => \&process_file, no_chdir => $dir_count }, $dirfile); sub process_file { if (-f $_) { #print "This is a file: $_\n"; $file = $_; $file =~ s/.*\///; #$file =basename($file); $file =~ s/\.[^.]+$//; #print "FileName without path [$file]\n"; my @filenames =$file; print @filenames; print "\n"; } } print "\n"; #logic to replace all space to comma delimited from a file,but this logic should be placed once if file is found from above directory search... # below $dir path is just to for checking purpose.. $dir = $base_dir.'/'.$dir_names[0].'/'.$dir_names[1].'/'.$dir_names[2]; #print $dir; open $resultfile, '>>', 'C:\Test' or die "Can't open file: $!"; #foreach (@dir_names){ foreach $fp (glob("$dir/*.bat")) { 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':"; } #### start putty -ssh -W 13633 1.0.7.1 -l maxi -qaq sooter #### # finally the xml structure to print/output in a file DIR2A's-FILE NAME SSH FILE1's COLUMN[6] FILE1's COLUMN[5] Default Settings FILE1's COLUMN[10] DIR2A's-FILE NAME SSH FILE1's COLUMN[6] FILE1's COLUMN[5] Default Settings FILE1's COLUMN[10] DIR2B's-FILE1 NAME SSH FILE1's COLUMN[6] FILE1's COLUMN[5] Default Settings FILE1's COLUMN[10] DIR2B's-FILE NAME SSH FILE2's COLUMN[6] FILE2's COLUMN[5] Default Settings FILE2's COLUMN[10] DIR3B's-FILE1 NAME SSH FILE1's COLUMN[6] FILE1's COLUMN[5] Default Settings FILE1's COLUMN[10] DIR3B's-FILE NAME SSH FILE2's COLUMN[6] FILE2's COLUMN[5] Default Settings FILE2's COLUMN[10]