[#1018] Move check for existence to *after* the check for /foo/

We never reach the case where we check whether $file is a directory,
because we always go next if it's not a $file.

Fixes #1018.