Javascript FSO OpenTextFile Method

Updated on 28 Mar 2012,
Published on 06 Oct 2008

Javascript FileSystemObject (FSO) OpenTextFile method provides the functionality to open or create a text file on the client’s machine. OpenTextFile method is similar to CreateTextFile method of FileSystemObject but there is a difference between them. OpenTextFile file function can create a file if the filename does not exist at the specified location and opens it immediately for reading or writing whereas CreateTextFile method creates a file and returns its texstream. You can use only write methods of textstream after creating a file using CreateTextFile method. On the otherhand, OpenTextFile method supports all the methods of textstream such as read, write and all the properties of textstream to get column number, line number etc.

Syntax for Javascript FSO OpenTextFile method

fso.OpenTextFile(filename, [IOMode], [Create], [Format]);

FSO OpenTextFile method accepts 4 types of parameters as shown in the above syntax.

1. Filename: this parameter accepts the filename along with its complete path where you want to create a text file or open the specified text file.

2. IOMode: Optional input output mode. This parameter allows you to open the file for reading, writing or appending. IOMode parameter supports following set of values:

1 = ForReading : for reading the text file

2 = ForWriting : for writing into the text file. This value opens the file and writes from the beginning i.e. first line.

8 = ForAppending : also for writing into the text file. This value opens the file but writes at the end of the file.

3. Create: Optional. Accepts the Boolean value i.e. true or false that indicates whether to create a new file if the specified file does not exist. If true then it creates a new file otherwise not. Default is true.

4. Format: Optional. Format of file as Unicode or ASCII. This parameter supports following values:

0 = TristateFalse: Open the file as ASCII (Default value)

-1 = TristateTrue: Open the file as Unicode

-2 = TristateMixed: Mixed mode

-2 = TristateUseDefault: Open the file as System Default type.

Example of FSO OpenTextFile Method

<script type="text/javascript">
    // initialize ActiveXObject and create an object of Scripting.FileSystemObject.
    var fso = new ActiveXObject("Scripting.FileSystemObject");

    // Open the text file at the specified location with append mode
    var txtFile = fso.OpenTextFile("C:\\Temp\\myFolder\\file.txt", 8, true, 0);
    txtFile.WriteLine("Hello this is the first line of text file.");
    txtFile.WriteLine("Hello this is the second line of text file.");
    txtFile.WriteLine("Hello this is the third line of text file.");


    fso = null;

Also read the tutorial: Javascript FSO CreateTextFile Method to learn about the file object properties and methods.

Continue to next tutorial: Javascript FSO textstream Object AtEndOfLine property to learn how to check whether textstream pointer is at the last line or not.

1 Responses to "Javascript FSO OpenTextFile Method"
nice post.. ^^.. I've been looking for this code.. then found out that this is easy to use.. thanks.. this help me very much.. ^^.
Leave a Comment
* required
* required
* will not be published
* optional
* hint:
  • Subscribe via Email