my @todo = glob('d:/Profiles/sal/Desktop/testFolder/*.bld'); # some kind of file my %done; undef $/; while(@todo) { my $file = shift @todo; print "File Name : $file\n"; open FILE, "+<$file" or die "Can't open file $file: $!"; $_ = ; # whole contents of file if(s/\bold\b/new/g) { # replace contents of file seek FILE, 0, 0; truncate FILE, 0; print FILE; } my($dir) = $file =~ m[(.*/)]; while(/^#include (\S+);/mg) { push @todo, "$dir$1" unless $done{"$dir$1"}++; } } #### -hi.bld -do.bld +one.bld