How to write text to a file

ImportantThe following article uses options that are available starting with the Freeware edition and project type.

NoteThis How-To explains how to write data into a simple text file. If you need to write information into an XML or INI file, you can use the XML Editor or the INI Editor

Sometimes the installation package must write some information into a file installed by your package. This can be done using a custom action which calls the OpenTextFile method.

Lets suppose that after the installation is complete you want to have a file which contains the serial number entered by the user. This serial number is stored in the SERIAL_NUMBER property, which is assigned to an edit box control on a custom dialog.

1. Configure the file

First you need to add the file into the Files and Folders page in the folder you want to install it into. Since the user can configure the installation folder, you need to set the path of the file into a public property. For this, you go to the Custom Actions page and create a new Set installer property custom action with sequence under Install Execution Stage -> Add Resources. The custom action can use these fields:

  • Property: SERIAL_FILE
  • Value: [APPDIR]serial.txt
  • Execution Time: Immediately
  • Condition: NOT Installed

Basically, this custom action sets the "SERIAL_FILE" property to the path of the "serial.txt" folder. In this example the file is placed in "Application Folder".

Caution!This custom action should be scheduled before the custom action which writes into the file.

2. Configure the custom action

A simple VBScript custom action which writes the information in the text file is this:

   Const ForReading = 1, ForWriting = 2, ForAppending = 8

   Dim fso, f
   Dim tokens
   Dim file

   file=Session.Property("CustomActionData")
   tokens=Split(file,"|")

   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile(tokens(0), ForWriting, True)

   f.Write tokens(1)
   f.Close

You can add this custom action as a "Launch attached file" custom action with sequence under Install Execution Stage -> Add Resources. Also, you can use these settings:

  • Attached File: <path_to_vbs_file>
  • File type: Visual Basic Script (*.vbs)
  • Function: <empty> (the script doesn't contain a function)
  • Action Data: [SERIAL_FILE]|[SERIAL_NUMBER] (this field sets the "CustomActionData" property)
  • Execution Time: When the system is being modified (deferred)
  • Execution Options: all options checked
  • Condition: NOT Installed

The custom action uses the file variable to get the [SERIAL_FILE]|[SERIAL_NUMBER] string which contains the path of the file, a pipe character ("|") and the serial number. The pipe character is used to parse the string and set multiple installer properties from it.

The tokens variable is used to split the string into two values: the path of the file (tokens(0)) and the serial number(tokens(1)).

This How-to writes data into a file installed by your package. If you want to create the file with the custom action, you can use the CreateTextFile method.