Check if directory is empty before deleting it (closes #971)