Javascript FileSystemObject (FSO) provides a method CreateTextFile that allows you to create a text file at the specified location and file name on the client’s PC. CreateTextFile method creates a text file and returns the textstream of the newly created file that allows you to read and write into the text file. Javascript FSO CreateTextFile method returns the object for textstream that can be used to access the properties and methods associated with file stream.
Syntax for Javascript FSO CreateTextFile method
fso.CreateTextFile(FileName, [Overwrite], [Unicode]);
FSO CreateTextFile method accepts 3 types of parameters:
1. FileName: this parameter accepts the location and file name that is to be created.
2. Overwrite: optional. This parameter accepts a Boolean value as 0/1 or true/false. If true then CreateTextFile method overwrites the existing file other it does not overwrite the existing file. By Default overwrite parameter value is True.
3. Unicode: optional. This parameter accepts a Boolean value as 0/1 or true/false. If true then CreateTextFile method creates a file as Unicode otherwise it creates a file as ASCII. By Default value for Unicode parameter is false.
Example for Javascript FSO CreateTextFile method
<script type="text/javascript">
// initialize ActiveXObject and create an object of Scripting.FileSystemObject.
var fso = new ActiveXObject("Scripting.FileSystemObject");
// creates a text file at the specified location with specified name and returns a textstream object
var myFile = fso.CreateTextFile("C:\\Temp\\myFolder\\file.txt");
// closes the currently open text file stream
myFile.Close();
fso = null;
</script>
Javascript CreateTextFile method of FSO further provides the access to the properties and methods of textstream associated with file object. Following are the properties and methods of File Object textstream:
Properties
- AtEndOfLine: returns the Boolean result i.e. true or false if file pointer while reading a text file using textstream object is at the end of line.
- AtEndOfStream: returns Boolean result i.e. true or false if file pointer is at the end of text file stream.
- Column: returns the number value as column number of the current character in a textstream file.
- Line: returns the number value as line number of the current character in textstream of a file.
Methods
- Close: closes the textstream file opened in the memory.
- Read: this method read a specific number of characters as a string.
- ReadAll: reads the entire text stream into a string.
- ReadLine: reads a single line of text stream file into a string.
- Skip: skips a specific number of characters while reading a text file.
- SkipLine: skips a whole line while reading a textstream file.
- Write: writes a specified string into the text file.
- WriteBlankLines: writes a specified number of blank lines into the textstream file.
- WriteLine: writes a specified string and adds end of line i.e. new line character at its end into a text file stream.
Continue to next tutorial: Javascript FSO DeleteFile Method to learn how to delete a file from client’s PC.