FileTextStream Interface
The Text Stream object (IFileTextStream) is returned by the CreateTextFile and OpenTextFile functions of the ComScript FileObject. Through this object the data in the file can be accessed as text with the use of the properties and functions described below.
Properties
Name |
Return Type |
Description |
EndOfFile |
Boolean |
True when during reading the end of the file has been reached. |
Functions
Name |
Return Type |
Parameters |
Description |
|
Name |
Type |
|||
Close |
Boolean |
--- |
Closes the current file. Returns True when successful. |
|
Read |
String |
NumberToRead |
Integer |
Reads the number of characters specified by Count from the stream and returns them as a string. A next Read will start at the point in the stream where the previous Read stopped, until the end of the file has been reached. |
ReadLine |
String |
--- |
Reads the next line from the stream and returns the line as a string until the end of the file has be
reached. The end of a line is defined by a CR/LF sequence in the file. The CR/LF sequence will not
be part of the returned string. |
|
Write |
Boolean |
Text |
String |
Writes the string specified in Text to the end of the stream. Returns True when successful
and false when failed. When flush is set to True the pending information in the stream will be written to the file directly and the internal stream buffers will be cleared. |
Flush |
Boolean |
|||
WriteLine |
Boolean |
Text |
String |
Writes the string specified in Text to the end of the stream and adds a CR/LF at the end.
Returns True when successful and false when failed. When flush is set to True the pending information in the stream will be written to the file directly and the internal stream buffers will be cleared. |
Flush |
Boolean |
|||
WriteLineAsync | Void | Text | String | As WriteLine but with an Async call. |
Flush | Boolean |
Example
PowerShell
#See ComScript Connection Interface
...
$ComScr.Connect()
#Create an object of the FileObject Interface to be able to handle files.
$AmtFile = $ComScr.CreateFileObject()
#Open a text file, this creates an object of the FileTextStream Interface to be
able to read from and write to text files.
$TextFile = $AmtFile.OpenTextFile("D:\Files\Data.txt", 3, 1, $true)
#Create a new text file, this creates a second object of the FileTextStream
Interface
$NewTextFile = $AmtFile.CreateTextFile("D:\Files\NewData.txt", 1, $false, $true)
#Read 10 lines of the first file and write them to the second file.
For ($i = 0; $i -lt 10; $i++) {
$TextLine = $TextFile.ReadLine()
[void]$NewTextFile.WriteLine($TextLine, $true)
}
#Close both text files.
[void]$TextFile.Close()
[void]$NewTextFile.Close()
# Finally release the variables and clean up the session.
$ComScr.Dispose()