File and FTP Interfaces

Using the content of a message to form the output file name

This example shows how easy it is to use the built Lua ‘io’ library to use the message control ID to form the file name of the message. This is just one example:

Note: The main() function uses print() to log messages. When an Iguana channel is run print() messages are logged as informational messages. For more sophisticated logging you can use the iguana.logInfo(), iguana.logWarning(), iguana.logDebug() functions.

Because of the dynamic nature of the Translator environment it’s easy to:

  • Use data from any source to define the file name
  • Dynamically see the generated file name
  • Actually verify that the resulting file is generated as you would like

It makes for a beautifully productive work flow and it’s easy to see the flow of data. Rather cool. Here’s the code in a format you can copy. Enjoy!

require("node")

function main(Data)
   local Msg = hl7.parse{data=Data, 
             vmd='example/demo.vmd'}
   local FileName = Msg.MSH[10]..'.hl7'
   local F = io.open('D:temp'..FileName, "w")
   F:write(Data)
   F:close()
   print('Wrote "'..FileName..'"')
end

Tip: The file actions are active in the editor (test mode), which means you can create a large number of “test” files. Once your code is working correctly you may want to prevent this. Also you will need to disable this for a live system, to prevent accidental updates.

The solution is simply to use iguana.isTest() to prevent the code from running.

Leave A Comment?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.