When writing a set of files, only write to valid directories.

This fixes issue #40