combine_text.pl

combines from list of file names in combine.txt:

#!/usr/bin/perl

use strict;
use warnings;
use File::Spec;

# Assume the current directory contains this script, combine.txt, and the files to be combined
my $folder_path = File::Spec->rel2abs('.');

# File containing the list of filenames to combine
my $list_file = "$folder_path/combine.txt"; # Path to combine.txt in the same folder

# Output file where the combined content will be stored
my $output_file = "$folder_path/combined_output_02.txt"; # Output file in the same folder

# Clear the contents of the output file before appending new content
open(my $clear_fh, '>', $output_file) or die "Cannot open $output_file for writing: $!";
close($clear_fh);

# Open the output file for appending
open(my $out_fh, '>>', $output_file) or die "Cannot open $output_file for appending: $!";

# Open and read the list file containing the filenames to combine
open(my $list_fh, '<', $list_file) or die "Cannot open $list_file for reading: $!";
my @files_to_combine = <$list_fh>;
chomp(@files_to_combine); # Remove newline characters from each filename
close($list_fh);

# Iterate through each file listed in combine.txt, read its content, and append to the output file
foreach my $file (@files_to_combine) {
    my $file_path = "$folder_path/$file"; # Construct the full path for each file
    open(my $in_fh, '<', $file_path) or die "Cannot open $file_path for reading: $!";
    while (my $line = <$in_fh>) {
        print $out_fh $line; # Append the content of the file to the output file
    }
    close($in_fh);
}

# Close the output file handle
close($out_fh);

print "Combined files listed in $list_file into $output_file\n";