#!/usr/bin/perl use strict; use warnings; my $dir = "."; my $root= "c:\\mlx\\myfile.txt"; lista_dirs($dir, $dir); sub lista_dirs { my ($dir, $dirname) = @_; my (@dirs, @files); opendir DIR, "$dir"; my @dircontent = grep { /[^\.]/ } readdir(DIR); closedir DIR; @dirs = (); @files = (); foreach(@dircontent) { if(-d "$dir/".$_) { push @dirs, $_; } else { push @files, $_ if($_ =~ /\.txt$/); } } foreach my $d(@dirs) { lista_dirs("$dir/".$d, $d); } # IMPORTANT NOTE: if your files aren't too big, you can read everything at once as shown in the code below. # if note, you can open the input file for reading and a temp file for wirting. Read the file line by line, # replace \n with the , and write to the temp file. Close both files when you're done, and then 'rename' # the temp file to the input filename foreach my $file (@files) { $/ = ''; open FILE, "$dir/$file"; my $content = ; close FILE; $content =~ tr/\n/,/; open FILE, ">$dir/$file"; print FILE $content; open FILE2, ">>$root"; print FILE2 $content; close FILE; close FILE2; } return; }