opendir (TXT, "$processedDirPath") or die "Cannot open $processedDirPath directory $!\n"; while ( (my $matchingFiles = readdir(TXT)) ) { if ( $matchingFiles =~ /\.rtsd001$/ ) { my @fileContents; my $UNMODIFIED; my $MODIFIED; open(UNMODIFIED, "<$processedDirPath/$matchingFiles") or die "Cannot open $UNMODIFIED for reading $!\n"; open(MODIFIED, ">$processedDirPath/$matchingFiles.old") or die "Cannot open $MODIFIED for writing $!\n"; while () { /STRUC20/ and @fileContents=(), next or push @fileContents, $_; # All files that end in rtsd001 will need to be modified. } print MODIFIED @fileContents; close(UNMODIFIED); close(MODIFIED); system( "/bin/mv", "$processedDirPath/$matchingFiles.old", "$processedDirPath/$matchingFiles" ) == 0 or warn "Move command filed $!\n";; } } closedir TXT;