#! /usr/bin/perl use strict; use warnings; # https://www.perl-community.de/bat/poard/thread/18288 use File::Find; # for searching for files/directories use File::Spec::Functions qw( catfile ); # for creating portable file-paths use Cwd; # for determing current work directory my @directories = ( cwd, # search in current work directory ); my %match; # storage for matching file paths sub combine_to { my $newfile = shift; my $directory = shift; my $filesRef = shift; my $outfile = catfile( $directory, $newfile ); open my $wh, '>', $outfile or die "open(w,$outfile) failed: $!\n"; for my $file ( @$filesRef ) { my $infile = catfile( $directory, $file ); open my $rh, '<', $infile or die "open(ro,$infile) failed: $!\n"; print $wh (<$rh>); close $rh; } close $wh or die "close($outfile) failed: $!\n"; } sub find_ffn_files { return if ! -d $File::Find::name; # skip if not a directory my $dir = $File::Find::name; # short name of directory opendir my $dh, $dir or die "Cannot open '$dir': $!\n"; # read '.ffn' files from directory and create full file path my @files = grep { m/\.ffn$/ } readdir $dh; closedir $dh; # create hash of array for matches; we must have found exactly 2 files $match{$dir} = \@files if 2 == @files; } # search for files and fill @matches find( \&find_ffn_files, @directories ); # check %match for my $dir ( keys %match ) { # combine found files into 'combined.ffn' in corresponding directory combine_to( 'combined.ffn', $dir => $match{$dir} ); }