Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: TextStream and Scripting.FileSystemObject
; =============================================================================================================================== ; 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. ; otfCreate = @true otfNoCreate = @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. ; otfDefault = -2 ; Opens the file using the system default. otfUnicode = -1 ; Opens the file as Unicode. otfAscii = 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. ; ctfOverwrite = @true ctfNoOverwrite = @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. ; ctfUnicode = @true ctfAscii = @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? ; ------------------------------------------------------------------------------------------------------------------------------- ; ; ------------------------------------------------------------------------------------------------------------------------------- WILVersionNeeded = "3.6dcf" WILVersion = VersionDll() If (WILVersion < "3.6dcf") msgtitle = StrCat("WIL Interpreter Version ",WILVersion) msgtext = StrCat("Script needs Version ",WILVersionNeeded,@crlf,"Otherwise some OLE operations may do not work properly.") pause(msgtitle,msgtext) EndIf ; ------------------------------------------------------------------------------------------------------------------------------- fso = ObjectOpen("Scripting.FileSystemObject") ; ------------------------------------------------------------------------------------------------------------------------------- :test1 ; create a textfile and write some lines. testfile = "d:\temp\test.ascii.txt" MyFile = fso.CreateTextFile(testfile, ctfOverwrite) ; Create a file as a TextStream MyFile.WriteLine("This is a test. Line 1.") ; Write a string and an end of line to the stream. MyFile.WriteBlankLines(2) ; Write a number of blank lines to the stream. MyFile.WriteLine("This is a test. Line 4.") ; Write a string and an end of line to the stream. MyFile.Write("This is a test. Line 5.") ; Write a string to the stream. MyFile.Write("This is a test. Line 5.") ; Write a string to the stream. MyFile.Write("This is a test. Line 5.") ; Write a string to the stream. MyFile.WriteLine ; Write an end of line to the stream. MyFile.WriteLine("This is a test. Line 6.") ; Write a string and an end of line to the stream. MyFile.Close ; Close a text stream. ObjectClose(MyFile) ; ------------------------------------------------------------------------------------------------------------------------------- :test2 ; read lines, skip lines, skip chars, read chars. testfile = "d:\temp\test.ascii.txt" MyFile = fso.OpenTextFile(testfile, ForReading) ; Open a file as a TextStream ReadLineTextFile = MyFile.ReadLine ; line 1 ; Read an entire line into a string. MyFile.SkipLine ; line 2 ; Skip a line. MyFile.SkipLine ; line 3 ; Skip a line. ReadLineTextFile = MyFile.ReadLine ; line 4 ; Read an entire line into a string. MyFile.Skip(6) ; line 5 skip 6 chars ; Skip a specific number of characters. ReadSomeChars = MyFile.Read(10) ; line 5 read 10 chars from line 5. ReadLineTextFile = MyFile.ReadLine ; line 5 read the rest of the line. ReadLineTextFile = MyFile.ReadLine ; line 6 ; Read an entire line into a string. MyFile.Close ; Close a text stream. ObjectClose(MyFile) ; ------------------------------------------------------------------------------------------------------------------------------- :test3 ; read entire file at once, count lines. testfile = "d:\temp\test.ascii.txt" MyFile = fso.OpenTextFile(testfile, ForReading) ; Open a file as a TextStream ReadAllTextFile = MyFile.ReadAll ; Read the entire stream into a string. LineCount = MyFile.Line ; Current line number. ; Note: ; If MyFile ends without a @crlf sequence, then LineCount contains the actual last line number. ; If MyFile ends with a @crlf sequence, then LineCount contains a number of actual lines plus 1. MyFile.Close ; Close a text stream. ObjectClose(MyFile) ; ------------------------------------------------------------------------------------------------------------------------------- :test4 ; read single chars from a line, count chars. testfile = "d:\temp\test.ascii.txt" MyFile = fso.OpenTextFile(testfile, ForReading) ; Open a file as a TextStream ThisLine = "" While !MyFile.AtEndOfLine ; Is the current position at the end of a line? ThisColumnCount = MyFile.Column ; Current column number. ThisLine = StrCat(ThisLine, MyFile.Read(1)) ; Read a specific number of characters into a string. NextColumnCount = MyFile.Column ; Current column number. EndWhile MyFile.Close ; Close a text stream. ObjectClose(MyFile) ; Note: ; After reading the last char in line with MyFile.Read(n) ; then MyFile.Column points to one char beyond the actual end of text (n+1). ; Therefore the end of line has to be detected explicitely by MyFile.AtEndOfLine. ; ------------------------------------------------------------------------------------------------------------------------------- :test5 ; read lines, count lines. testfile = "d:\temp\test.ascii.txt" MyFile = fso.OpenTextFile(testfile, ForReading) ; Open a file as a TextStream While !MyFile.AtEndOfStream ; Is the current position at the end of the stream? ThisLineCount = MyFile.Line ; Current line number. ThisLine = MyFile.ReadLine ; Read an entire line into a string. NextLineCount = MyFile.Line ; Current line number. EndWhile MyFile.Close ; Close a text stream. ObjectClose(MyFile) ; Note: ; After reading one line then MyFile.Line points to the next line number. ; If the file ends without a @crlf sequence, then MyFile.Line contains the actually last line number. ; If the file ends with a @crlf sequence, then MyFile.Line points to the last line plus one. ; Therefore the end of file has to be detected explicitely by MyFile.AtEndOfLine. ; ------------------------------------------------------------------------------------------------------------------------------- :test6 ; read ascii file, write unicode file, convert without explicitely buffering asciifile = "d:\temp\test.ascii.txt" unicodefile = "d:\temp\test.unicode.bin" MyAsciiFile = fso.OpenTextFile(asciifile, ForReading, otfNoCreate, otfAscii) MyUnicodeFile = fso.CreateTextFile(unicodefile, ctfOverwrite, ctfUnicode) MyUnicodeFile.Write(MyAsciiFile.ReadAll) MyAsciiFile.Close MyUnicodeFile.Close ObjectClose(MyAsciiFile) ObjectClose(MyUnicodeFile) run(StrCat(DirHome(),"browser.exe"),asciifile) run(StrCat(DirHome(),"browser.exe"),unicodefile) ; ------------------------------------------------------------------------------------------------------------------------------- :test7 ; read a number of chars from unicode text stream, count chars. testfile = "d:\temp\test.unicode.bin" MyFile = fso.OpenTextFile(testfile, ForReading, otfNoCreate, otfUnicode) ; Read Unicode ; Open a file as a TextStream MyFile.Skip(80) ; Skip first 80 chars ; Skip a specific number of characters. ThisStream = MyFile.Read(1000) ; Read 1000 chars ; Read a specific number of characters into a string. Length = StrCharCount(ThisStream) ; How many chars actually read? MyFile.Close ; Close a text stream. ObjectClose(MyFile) ; ------------------------------------------------------------------------------------------------------------------------------- ObjectClose(fso) Exit ; ===============================================================================================================================
Article ID: W15590
File Created: 2003:05:13:11:29:08
Last Updated: 2003:05:13:11:29:08