Error-check directory creation and file writing.