grashoper has asked for the wisdom of the Perl Monks concerning the following question:
#!/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 every +thing 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 = <FILE>; close FILE; $content =~ tr/\n/,/; open FILE, ">$dir/$file"; print FILE $content; open FILE2, ">>$root"; print FILE2 $content; close FILE; close FILE2; } return; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: concatenate directories and files into one csv
by jethro (Monsignor) on Apr 28, 2009 at 16:40 UTC | |
|
Re: concatenate directories and files into one csv
by mr_mischief (Monsignor) on Apr 28, 2009 at 16:41 UTC | |
|
Re: concatenate directories and files into one csv
by almut (Canon) on Apr 28, 2009 at 16:42 UTC | |
|
Re: concatenate directories and files into one csv
by ikegami (Patriarch) on Apr 28, 2009 at 17:33 UTC | |
|
Re: concatenate directories and files into one csv
by GrandFather (Saint) on Apr 28, 2009 at 22:27 UTC |