We shall use File System object to open a File and read its content through 'Read','ReadAll'
and 'ReadLine' methods of a File, opened in Reading mode.
We can open a file in three different mode for reading ,writing and appending.Const is a reserved
keyword in VB Script which is used to declare constant variables in script.OpenTextFile
is method of FileSystemObject which is used to open files.
1.'Example of Read method of File object, Read Method takes parameter as number of
specified characters that should be read
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim objFso, objFile, Msg
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile= objFso.OpenTextFile("D:\Music\myTextFile.txt", ForWriting, True)
'Write content into the file
objFile.Write "Hello world!"
Set objFile = objFso .OpenTextFile("D:\Music\myTextFile.txt", ForReading)
OutPut:
'2.ReadLine() method reads first line in file and returns resulting string
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim objFso, objFile, Msg
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile= objFso.OpenTextFile("D:\Music\myTextFile.txt", ForWriting, True)
'Write content into the file
objFile.Write "Hello world!"
Set objFile = objFso .OpenTextFile("D:\Music\myTextFile.txt", ForReading)
3.ReadAll() method of File object returns all the lines available in File
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim objFso, objFile, Msg
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile= objFso.OpenTextFile("D:\Music\myTextFile.txt", ForWriting, True)
'Write content into the file
objFile.Write "Hello world!"
'write another line to understand better
objFile.WriteBlankLines(1)
objFile.Write"Hello Leela!"
Set objFile = objFso.OpenTextFile ("D:\Music\myTextFile.txt", ForReading)
Msgbox objFile.ReadAll()
and 'ReadLine' methods of a File, opened in Reading mode.
We can open a file in three different mode for reading ,writing and appending.Const is a reserved
keyword in VB Script which is used to declare constant variables in script.OpenTextFile
is method of FileSystemObject which is used to open files.
1.'Example of Read method of File object, Read Method takes parameter as number of
specified characters that should be read
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim objFso, objFile, Msg
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile= objFso.OpenTextFile("D:\Music\myTextFile.txt", ForWriting, True)
'Write content into the file
objFile.Write "Hello world!"
Set objFile = objFso .OpenTextFile("D:\Music\myTextFile.txt", ForReading)
Msgbox objFile.Read(5) 'display first five characters of file
OutPut:
'2.ReadLine() method reads first line in file and returns resulting string
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim objFso, objFile, Msg
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile= objFso.OpenTextFile("D:\Music\myTextFile.txt", ForWriting, True)
'Write content into the file
objFile.Write "Hello world!"
Set objFile = objFso .OpenTextFile("D:\Music\myTextFile.txt", ForReading)
Msgbox objFile.ReadLine()
Output:
3.ReadAll() method of File object returns all the lines available in FileConst ForReading = 1, ForWriting = 2, ForAppending = 8
Dim objFso, objFile, Msg
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile= objFso.OpenTextFile("D:\Music\myTextFile.txt", ForWriting, True)
'Write content into the file
objFile.Write "Hello world!"
'write another line to understand better
objFile.WriteBlankLines(1)
objFile.Write"Hello Leela!"
Set objFile = objFso.OpenTextFile ("D:\Music\myTextFile.txt", ForReading)
Msgbox objFile.ReadAll()
0 comments: