Fixes #5422: add generic method to check if a directory exists