#! /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':"; }