# \[Bug-11645\] Message Handler Write File

**URL:** <https://forum.inductiveautomation.com/t/bug-11645-message-handler-write-file/20855>\
**Category:** Ignition Early Access\
**Created:** [November 14, 2018, 4:48pm UTC](https://forum.inductiveautomation.com/t/bug-11645-message-handler-write-file/20855 "2018-11-14T16:48:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![christopher.green](https://avatars.discourse-cdn.com/v4/letter/c/b9bd4f/32.png) [@christopher.green](https://forum.inductiveautomation.com/u/christopher.green)\
**Post date:** [November 14, 2018, 4:48pm UTC](https://forum.inductiveautomation.com/t/bug-11645-message-handler-write-file/20855/1 "2018-11-14T16:48:43Z")

</div>

So I’m using a message handler to write a file on the gateway server. Since upgrading to 8.0 I get a logged error saying “unable to create file”, but the file is created just empty. If I run the script again I don’t get the error, and the file is overwritten with information. It will only work if the file already exists, the first time around it creates an empty file and throws an error, second time it works without complaint.

---

<div class="post-metadata">

**Author:** ![ggross](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ggross/32/12668_2.png) [@ggross](https://forum.inductiveautomation.com/u/ggross)\
**Post date:** [November 14, 2018, 9:21pm UTC](https://forum.inductiveautomation.com/t/bug-11645-message-handler-write-file/20855/2 "2018-11-14T21:21:54Z")

</div>

Can you post the specific syntax you are using where the file was automatically being created in 7.9? I am not seeing the behavior you are describing as working in 7.9 and in the [documentation](https://docs.inductiveautomation.com/display/DOC79/system.file.writeFile) for the **system.file.writeFile** function, it states that the file is written to if it exists. I just want to confirm that we are talking about the same function.

Thanks,  
Garth

---

<div class="post-metadata">

**Author:** ![christopher.green](https://avatars.discourse-cdn.com/v4/letter/c/b9bd4f/32.png) [@christopher.green](https://forum.inductiveautomation.com/u/christopher.green)\
**Post date:** [November 14, 2018, 9:34pm UTC](https://forum.inductiveautomation.com/t/bug-11645-message-handler-write-file/20855/3 "2018-11-14T21:34:46Z")

</div>

```
fileName = payload['fileName']
fileDir = ''
fileDir = 'D:\Templates\ ' + fileName

fileText = payload["xml"]
if fileText != '':
	system.file.writeFile(fileDir, fileText, 0)

```

First pass it fails, creates an empty file, second pass it populates the file with data.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 14, 2018, 9:37pm UTC](https://forum.inductiveautomation.com/t/bug-11645-message-handler-write-file/20855/4 "2018-11-14T21:37:54Z")

</div>

This looks like a bug with the logic around creating “implicit” directories with writeFile. The change in behavior is due to the optional encoding arguments added to the system.file functions in 8.0. I’ve opened a ticket for it.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 14, 2018, 9:38pm UTC](https://forum.inductiveautomation.com/t/bug-11645-message-handler-write-file/20855/5 "2018-11-14T21:38:07Z")

</div>

Try changing to forward slashes and removing the extra space in fileDir.

---

<div class="post-metadata">

**Author:** ![christopher.green](https://avatars.discourse-cdn.com/v4/letter/c/b9bd4f/32.png) [@christopher.green](https://forum.inductiveautomation.com/u/christopher.green)\
**Post date:** [November 14, 2018, 9:52pm UTC](https://forum.inductiveautomation.com/t/bug-11645-message-handler-write-file/20855/6 "2018-11-14T21:52:16Z")

</div>

Same behavior when I remove spaces and go with forward slashes.

---

<div class="post-metadata">

**Author:** ![software.development](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/software.development/32/6688_2.png) [@software.development](https://forum.inductiveautomation.com/u/software.development)\
**Post date:** [November 16, 2018, 8:11am UTC](https://forum.inductiveautomation.com/t/bug-11645-message-handler-write-file/20855/7 "2018-11-16T08:11:08Z")

</div>

When will the fix be released?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [November 16, 2018, 1:48pm UTC](https://forum.inductiveautomation.com/t/bug-11645-message-handler-write-file/20855/8 "2018-11-16T13:48:34Z")

</div>

It should end up in a nightly build next week sometime.

---

<div class="post-metadata">

**Author:** ![awalker](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/awalker/32/6874_2.png) [@awalker](https://forum.inductiveautomation.com/u/awalker)\
**Post date:** [December 7, 2018, 12:38am UTC](https://forum.inductiveautomation.com/t/bug-11645-message-handler-write-file/20855/9 "2018-12-07T00:38:56Z")

</div>

This issue was fixed in the build that was uploaded on 11/17. Please let us know if you continue to see this behavior after upgrading.

---

<div class="post-metadata">

**Author:** ![christopher.green](https://avatars.discourse-cdn.com/v4/letter/c/b9bd4f/32.png) [@christopher.green](https://forum.inductiveautomation.com/u/christopher.green)\
**Post date:** [December 7, 2018, 3:36pm UTC](https://forum.inductiveautomation.com/t/bug-11645-message-handler-write-file/20855/10 "2018-12-07T15:36:48Z")

</div>

It has worked for me since that update.
