Write out text file on Gateway

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:

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.

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

filename = D:\BERRY\Test.txt"

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

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

fPath = "c:\\filename.csv"

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

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

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.

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:

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.)

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.

1 Like

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.