User Tools

Site Tools


tutorials:perl:combine_files.pl
combine_files.pl
# combine_files.pl
# The opposite of write_lines_to_files; 
# obtain an input directory and an output file 
# from the command line, read the files in the 
# input directory and write their content to 
# one single output file.
# Note that we want to exclude hidden files 
# in the input directory.
 
my $in_dir = shift;
my $out_file = shift;
 
open OUT, ">$out_file";
 
opendir( DIR, $in_dir ) or die "can't open $in_dir: $!\n";
foreach $in_file ( sort readdir(DIR) ) { 
 	next if ( $in_file =~ /^\./ );
	open IN, "$in_dir$in_file";
	while ( my $line = <IN> ) {
		print OUT "$line";
	}
	close IN;
}
closedir(DIR);
 
close OUT;
tutorials/perl/combine_files.pl.txt · Last modified: 2012/06/15 00:36 by chkuo