# Write out text file on Gateway

**URL:** <https://forum.inductiveautomation.com/t/write-out-text-file-on-gateway/7054>\
**Category:** Ignition\
**Created:** [August 15, 2013, 1:45pm UTC](https://forum.inductiveautomation.com/t/write-out-text-file-on-gateway/7054 "2013-08-15T13:45:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbackus](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tbackus/32/5091_2.png) [@tbackus](https://forum.inductiveautomation.com/u/tbackus)\
**Post date:** [August 15, 2013, 1:45pm UTC](https://forum.inductiveautomation.com/t/write-out-text-file-on-gateway/7054/1 "2013-08-15T13:45:00Z")

</div>

I have a need to write out a text file with some comma delimited information, so that my ERP system can grab that text file and process it.

I am using:

```auto
import fileinput
filename = system.file.saveFile("D:\BERRY\Test.txt")
#####
## Some Code to create the text file ##
#####
system.file.writeFile(filename,adjScanRead,1)
```

When I activated this as a button on the client I got a pop up asking me to basically confirm filename and location, and when I did that was able to see the file on my client without issue.

My problem is when I put this in as a gateway script to execute on an OPC tag change, nothing at all seems to happen. the test.txt file stays empty, I get no alerts on the screen.

Am I going about this the wrong way? The idea is to have this text file continually appended to and put into a directory on the gateway so that my ERP system can grab it every 15 minutes and process it.

Thanks for any assistance.

---

<div class="post-metadata">

**Author:** ![george](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/george/32/14066_2.png) [@george](https://forum.inductiveautomation.com/u/george)\
**Post date:** [August 15, 2013, 2:48pm UTC](https://forum.inductiveautomation.com/t/write-out-text-file-on-gateway/7054/2 "2013-08-15T14:48:56Z")

</div>

system.file.saveFile prompts the user to save the file (so won’t work on the gateway).

I think you just need to get rid of it, i.e. replace with

```auto
filename = D:\BERRY\Test.txt"
```

Then your system.file.writeFile line will actually create the file.

---

<div class="post-metadata">

**Author:** ![adamaustin](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@adamaustin](https://forum.inductiveautomation.com/u/adamaustin)\
**Post date:** [August 22, 2013, 7:54pm UTC](https://forum.inductiveautomation.com/t/write-out-text-file-on-gateway/7054/3 "2013-08-22T19:54:28Z")

</div>

Your only problem in your code may be escaping the backslashes:

```auto
fPath = "c:\\filename.csv"

csvResult = system.dataset.dataSetToCSV(1,dsResult)
system.file.writeFile(fPath,csvResult,1) #use 1 here to append data
```

---

<div class="post-metadata">

**Author:** ![tbackus](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tbackus/32/5091_2.png) [@tbackus](https://forum.inductiveautomation.com/u/tbackus)\
**Post date:** [September 6, 2013, 2:23pm UTC](https://forum.inductiveautomation.com/t/write-out-text-file-on-gateway/7054/4 "2013-09-06T14:23:29Z")

</div>

Sorry guys been away from this project for a couple weeks… Anyway, tried these suggestions and have had no luck.

---

<div class="post-metadata">

**Author:** ![adamaustin](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@adamaustin](https://forum.inductiveautomation.com/u/adamaustin)\
**Post date:** [September 6, 2013, 4:34pm UTC](https://forum.inductiveautomation.com/t/write-out-text-file-on-gateway/7054/5 "2013-09-06T16:34:59Z")

</div>

That should work. I have a gateway event script that writes out to a text file every three hours then FTPs it up to a remote server, so maybe if you posted your code we can help you debug it.

---

<div class="post-metadata">

**Author:** ![tbackus](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tbackus/32/5091_2.png) [@tbackus](https://forum.inductiveautomation.com/u/tbackus)\
**Post date:** [September 10, 2013, 1:56pm UTC](https://forum.inductiveautomation.com/t/write-out-text-file-on-gateway/7054/6 "2013-09-10T13:56:06Z")

</div>

This code is set to trigger on tag change: _AHSK\_Conveyor_/ST16/ST16:1  
That tag is where a barcode scanner puts a raw tag read.

Every read adds a new line to the text file… Here is a sample of the text file:

```auto
2013,09,06,09,02,46,22,C5818567701224,BX,1
 2013,09,06,09,03,46,22,C5818567701225,BX,1
 2013,09,06,09,04,46,22,C5847918100565,BX,1
 2013,09,06,09,06,16,22,C5818567701226,BX,1
```

The actual script: (as you will be able to tell, I am not a real programmer, I just play one on TV.)

```auto
import datetime
import fileinput
filename = "D:\\BERRY\\Test2.txt"
scanRead = event.source.parent.getComponent('LstRdBxTxt').text
#### Format scanRead properly ####
adjScanRead = scanRead.replace("No Dot,,","BX,1")
#### Add Plant Code ####
plantCode = ',22'
##### ASCII Carriage Return ####
##### NOT NEEDED - lineBreak = u"\u000D" ####
#### Grab date and time from system in 24hr format ####
today = datetime.datetime.today()
##### Change DateTime to proper comma delimited ###
varToday = today.strftime('%Y,%m,%d,%H,%M,%S')
##### Write everything to a file (filename, value, append) ####
system.file.writeFile(filename,varToday,1)
system.file.writeFile(filename,plantCode,1)
system.file.writeFile(filename,adjScanRead,1)
```

Thanks guys.

---

<div class="post-metadata">

**Author:** ![adamaustin](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@adamaustin](https://forum.inductiveautomation.com/u/adamaustin)\
**Post date:** [September 10, 2013, 2:18pm UTC](https://forum.inductiveautomation.com/t/write-out-text-file-on-gateway/7054/7 "2013-09-10T14:18:34Z")

</div>

The code works fine (albeit, you may need a newline ‘\n’ at the end). There must be something wrong with your assumptions about the Tag Change.
