#!/usr/bin/perl use strict; my %count; my $directory = "c:\\Test"; opendir( DIR, $directory ) || die "Unable to open directory - $!\n"; my @files = grep /\.txt/, readdir( DIR ); closedir( DIR ); open( OUTFILE, ">> $directory\\COID_LIST.txt" ) || die "Unable to open write file! - $!\n"; foreach my $file (@files) { open( FH, "$directory\\$file" ) || die "Unable to open $file - $!\n"; while( ) { my $ID = substr( $_, 260, 5 ); print OUTFILE "$ID\n"; $count{$ID}++ if ( defined( $ID ) ); } close( FH ); } print "Number of company ID = " . scalar keys %count;