Page Date
2004-05-18
DD-Software
Kapitel zurück / previous Chapter
Main Index
 
Seite zurück / previous page
Backward
Seite vor / next page
Forward
 
Seitenanfang/TopOfPage
Top
Seitenende/EndOfPage
Bottom
MyWbtHelp current version

TextStream Methods and Properties of 'Scripting.FileSystemObject'


TextStream Methods and Properties of 'Scripting.FileSystemObject'.WBT   Version 1.02  2002:08:25

The following WinBatch script describes and makes use of the
OLE object 'Scripting.FileSystemObject'
as described in the Microsoft Platform SDK, May 2002 Edition.



; ===============================================================================================================================
; Short Overview: TextStream Methods and Properties of "Scripting.FileSystemObject"                        Detlev Dalitz.20020706
; ===============================================================================================================================
; The TextStream Object
; Facilitates sequential access to file.
; TextStream.{property  | method( )}
; ===============================================================================================================================
;
; -------------------------------------------------------------------------------------------------------------------------------
; OpenTextFile
; Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file.
; object.OpenTextFile(filename[, iomode[, create[, format]]])
; -------------------------------------------------------------------------------------------------------------------------------
; iomode
; Optional. Can be one of three constants: ForReading, ForWriting, or ForAppending.
ForReading      = 1     ; Open a file for reading only. You can't write to this file.
ForWriting      = 2     ; Open a file for writing.
ForAppending    = 8     ; Open a file and write to the end of the file.
; -------------------------------------------------------------------------------------------------------------------------------
; create
; Optional. Boolean value that indicates whether a new file can be created if the specified filename doesn't exist.
; If omitted, a new file isn't created.
Create          = @TRUE ; The value is True if a new file is created
;                       ; The value is False if it isn't created.
;
iOpenCreate     = @TRUE
iOpenNoCreate   = @FALSE
; -------------------------------------------------------------------------------------------------------------------------------
; format
; Optional. One of three Tristate values used to indicate the format of the opened file.
; If omitted, the file is opened as ASCII.
TristateUseDefault = -2 ; Opens the file using the system default.
TristateTrue       = -1 ; Opens the file as Unicode.
TristateFalse      =  0 ; Opens the file as ASCII.
;
iOpenDefault       = -2 ; Opens the file using the system default.
iOpenUnicode       = -1 ; Opens the file as Unicode.
iOpenAscii         =  0 ; Opens the file as ASCII.
; -------------------------------------------------------------------------------------------------------------------------------
;
; -------------------------------------------------------------------------------------------------------------------------------
; CreateTextFile
; Creates a specified file name and returns a TextStream object that can be used to read from or write to the file.
; object.CreateTextFile(filename[, overwrite[, unicode]])
; -------------------------------------------------------------------------------------------------------------------------------
; overwrite
; Optional. Boolean value that indicates whether you can overwrite an existing file.
; If omitted, existing files are not overwritten.
Overwrite       = @TRUE  ; The value is true if the file can be overwritten.  
;                        ; The value is false if it can't be overwritten.     
;
iCreateOverwrite    = @TRUE
iCreateNoOverwrite  = @FALSE
; -------------------------------------------------------------------------------------------------------------------------------
; unicode
; Optional. Boolean value that indicates whether the file is created as a Unicode or ASCII file.
; If omitted, an ASCII file is assumed.
Unicode         = @TRUE  ; The value is true if the file is created as a Unicode file.
;                        ; The value is false if it's created as an ASCII file.       
;
iCreateUnicode  = @TRUE
iCreateAscii    = @FALSE
; -------------------------------------------------------------------------------------------------------------------------------
;
; -------------------------------------------------------------------------------------------------------------------------------
; object = ObjectOpen("Scripting.FileSystemObject") ; WIL syntax
; ObjectClose(object)                               ; WIL syntax
;
; TextStream = object.CreateTextFile(filename[, overwrite[, unicode]])      ; Creates a file as a TextStream
; TextStream = object.OpenTextFile(filename[, iomode[, create[, format]]])  ; Opens a file as a TextStream
;
; TextStream.Close                       ; Close a text stream.
;
; TextStream.ReadAll                     ; Read the entire stream into a string.
; TextStream.ReadLine                    ; Read an entire line into a string.
; TextStream.Read (n)                    ; Read a specific number of characters into a string.
; 
; TextStream.Write (string)              ; Write a string to the stream.
; TextStream.WriteLine                   ; Write an end of line to the stream.
; TextStream.WriteLine (string)          ; Write a string and an end of line to the stream.
; TextStream.WriteBlankLines (n)         ; Write a number of blank lines to the stream.
; 
; TextStream.SkipLine                    ; Skip a line.
; TextStream.Skip (n)                    ; Skip a specific number of characters.
; 
; TextStream.Line                        ; Current line number.
; TextStream.Column                      ; Current column number.
; 
; TextStream.AtEndOfLine                 ; Boolean Value. Is the current position at the end of a line?
; TextStream.AtEndOfStream               ; Boolean Value. Is the current position at the end of the stream?
; -------------------------------------------------------------------------------------------------------------------------------
;
; -------------------------------------------------------------------------------------------------------------------------------
sWILVersionRequired = "3.6dcf"
sWILVersion = VersionDll()
If (sWILVersion<sWILVersionRequired)
sMsgTitle = StrCat("WIL Interpreter Version ",sWILVersion)
sMsgText  = StrCat("Script requires Version ",sWILVersionRequired,@LF,"Otherwise some OLE operations may do not work properly.")
Pause(sMsgTitle,sMsgText)
EndIf
; -------------------------------------------------------------------------------------------------------------------------------
oFS = ObjectOpen("Scripting.FileSystemObject")
; -------------------------------------------------------------------------------------------------------------------------------
:test1
; Create a textfile and write some lines.
sFilenameTest = "d:\temp\test.ascii.txt"
oMyFile = oFS.CreateTextFile(sFilenameTest, iCreateOverwrite)   ; Create a file as a TextStream
oMyFile.WriteLine("This is a test. Line 1.")            ; Write a string and an end of line to the stream. 
oMyFile.WriteBlankLines(2)                              ; Write a number of blank lines to the stream.
oMyFile.WriteLine("This is a test. Line 4.")            ; Write a string and an end of line to the stream. 
oMyFile.Write("This is a test. Line 5.")                ; Write a string to the stream.
oMyFile.Write("This is a test. Line 5.")                ; Write a string to the stream.
oMyFile.Write("This is a test. Line 5.")                ; Write a string to the stream.
oMyFile.WriteLine                                       ; Write an end of line to the stream. 
oMyFile.WriteLine("This is a test. Line 6.")            ; Write a string and an end of line to the stream. 
oMyFile.Close                                           ; Close a text stream.
ObjectClose(oMyFile)
; -------------------------------------------------------------------------------------------------------------------------------
:test2
; Read lines, skip lines, skip chars, read chars.
sFilenameTest = "d:\temp\test.ascii.txt"
oMyFile = oFS.OpenTextFile(sFilenameTest, ForReading)   ; Open a file as a TextStream
sReadLineTextFile = oMyFile.ReadLine                    ; line 1  ; Read an entire line into a string. 
oMyFile.SkipLine                                        ; line 2  ; Skip a line.
oMyFile.SkipLine                                        ; line 3  ; Skip a line.
sReadLineTextFile = oMyFile.ReadLine                    ; line 4  ; Read an entire line into a string. 
oMyFile.Skip(6)                                         ; line 5 skip 6 chars ; Skip a specific number of characters.
sReadSomeChars    = oMyFile.Read(10)                    ; line 5 read 10 chars from line 5.
sReadLineTextFile = oMyFile.ReadLine                    ; line 5 read the rest of the line.
sReadLineTextFile = oMyFile.ReadLine                    ; line 6  ; Read an entire line into a string. 
oMyFile.Close                                           ; Close a text stream.
ObjectClose(oMyFile)
; -------------------------------------------------------------------------------------------------------------------------------
:test3
; Read entire file at once, count lines.
sFilenameTest = "d:\temp\test.ascii.txt"
oMyFile = oFS.OpenTextFile(sFilenameTest, ForReading)   ; Open a file as a TextStream
sReadAllTextFile = oMyFile.ReadAll                      ; Read the entire stream into a string.
iLineCount       = oMyFile.Line                         ; Current line number.
; Note: 
; If oMyFile ends without a @crlf sequence, then iLineCount contains the actual last line number.
; If oMyFile ends with a @crlf sequence, then iLineCount contains a number of actual lines plus 1.
oMyFile.Close                                           ; Close a text stream.
ObjectClose(oMyFile)
; -------------------------------------------------------------------------------------------------------------------------------
:test4
; Read single chars from a line, count chars.
sFilenameTest = "d:\temp\test.ascii.txt"
oMyFile = oFS.OpenTextFile(sFilenameTest, ForReading)   ; Open a file as a TextStream
sLineThis = ""
While !oMyFile.AtEndOfLine                              ; Is the current position at the end of a line? 
   iColumnThis = oMyFile.Column                         ; Current column number.
   sLineThis   = StrCat(sLineThis, oMyFile.Read(1))     ; Read a specific number of characters into a string.
   iColumnNext = oMyFile.Column                         ; Current column number.
EndWhile
oMyFile.Close                                           ; Close a text stream.
ObjectClose(oMyFile)
; Note:
; After reading the last char in line with oMyFile.Read(n) 
; then oMyFile.Column points to one char beyond the actual end of text (n+1).
; Therefore the end of line has to be detected explicitely by oMyFile.AtEndOfLine.
; -------------------------------------------------------------------------------------------------------------------------------
:test5
; Read lines, count lines.
sFilenameTest = "d:\temp\test.ascii.txt"
oMyFile = oFS.OpenTextFile(sFilenameTest, ForReading)   ; Open a file as a TextStream
While !oMyFile.AtEndOfStream                            ; Is the current position at the end of the stream?
   iLineThis = oMyFile.Line                             ; Current line number.
   sLineThis = oMyFile.ReadLine                         ; Read an entire line into a string.
   iLineNext = oMyFile.Line                             ; Current line number.
EndWhile
oMyFile.Close                                           ; Close a text stream.
ObjectClose(oMyFile)
; Note:
; After reading one line then oMyFile.Line points to the next line number.
; If the file ends without a @crlf sequence, then oMyFile.Line contains the actually last line number.
; If the file ends with a @crlf sequence, then oMyFile.Line points to the last line plus one.
; Therefore the end of file has to be detected explicitely by oMyFile.AtEndOfLine.
; -------------------------------------------------------------------------------------------------------------------------------
:test6
; Read ascii file, write unicode file, convert without explicitely buffering .
sFilenameAscii     = "d:\temp\test.ascii.txt"
sFilenameUnicode   = "d:\temp\test.unicode.bin"

oMyFileAscii   = oFS.OpenTextFile(sFilenameAscii, ForReading, iOpenNoCreate, iOpenAscii)
oMyFileUnicode = oFS.CreateTextFile(sFilenameUnicode, iCreateOverwrite, iCreateUnicode)

oMyFileUnicode.Write(oMyFileAscii.ReadAll)

oMyFileAscii.Close
oMyFileUnicode.Close

ObjectClose(oMyFileAscii)
ObjectClose(oMyFileUnicode)

Run(StrCat(DirHome(),"browser.exe"),sFilenameAscii)
Run(StrCat(DirHome(),"browser.exe"),sFilenameUnicode)
; -------------------------------------------------------------------------------------------------------------------------------
:test7
; Read a number of chars from unicode text stream, count chars.
sFilenameTest = "d:\temp\test.unicode.bin"
oMyFile = oFS.OpenTextFile(sFilenameTest, ForReading, iOpenNoCreate, iOpenUnicode)  ; Read Unicode ; Open a file as a TextStream
oMyFile.Skip(80)                                       ; Skip first 80 chars ; Skip a specific number of characters.
sThisStream = oMyFile.Read(1000)                       ; Read 1000 chars ; Read a specific number of characters into a string.
iCharCount = StrCharCount(sThisStream)                 ; How many chars actually read?
oMyFile.Close                                          ; Close a text stream.
ObjectClose(oMyFile)
; -------------------------------------------------------------------------------------------------------------------------------
ObjectClose(oFS)
Exit
; ===============================================================================================================================


Page Date
2004-05-18
DD-Software
Kapitel zurück / previous Chapter
Main Index
 
Seite zurück / previous page
Backward
Seite vor / next page
Forward
 
Seitenanfang/TopOfPage
Top
Seitenende/EndOfPage
Bottom
MyWbtHelp current version