Added check if the file or directory exists before checking the stat because it may throw an error.