Add a better error message when a directory cannot be written to.

Closes #4607