Fix automatic filename generation when output is a directory