in reply to Why is "If" condition not working

-f $file1, with $file1 the basename of a file (as returned by readdir), can only return true if a file with that name exists in the current directory.

Your current directory is not the directory that you opened for scanning with readdir. That's why it (usually) returns false. If it returns true, it's because, by incident, a different with the same name exists in your current directory.