added directory creation to write-file function