Windows Installer, Java Installer, Freeware Installer
Home|Contact|Site Map|TOC|Search
Download  Features   Java  Licensing  Purchase  Testimonials  Support  Forums

How do I write text into a file?

Answer

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

Configure the file

First you need to add the file into the Files and Folders page in the folder you want to install it. 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 Property Source custom action under InstallExecuteSequence -> Install. The custom action can use these fields:

  • Property Name: SERIAL_FILE
  • Formatted Text: [APPDIR]serial.txt
  • Expression: 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".

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 "New Attached Custom Action" under InstallExecuteSequence -> Install. Also, you can use these settings:

  • Source Path: <path_to_vbs_file>
  • Source Type: Visual Basic Script (*.vbs)
  • Function Name: <empty> (the script doesn't contain a function)
  • Action Data: [SERIAL_FILE]|[SERIAL_NUMBER] (this field sets the "CustomActionData" property)
  • Execution Properties: Synchronous execution, check return code
  • Execution Options: Deferred with no impersonation (it must not use impersonation in order to run on Vista)
  • Expression: 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.

Privacy Policy | Windows Installer | Search Engine Ranking | Link Analyzer