my $file; while($file = readdir(DIR)) { ... #### my $file; while ($file = readdir(DIR)) { if($file =~ /\Amyname\.doc\z/i) { $bool = 1; last; } }