Read Contents of a Text File in VB Script

The contents of a text file can be read in three ways.

  • You can read a specific number of bytes from the file
  • You can read each line of the file (Each line terminated with newline character)
  • You can read the entire contents of the file at once. 

For these three kinds of reading there three different READ functions available in the file system object. They are i. 'Read' ii. 'ReadLine' and iii. 'ReadAll'

Reading a Specified Number of Bytes from the File (Using the 'Read' Function) 

Dim ObjFso
Dim StrFileName
Dim ObjFile
Dim StrData

StrFileName = "C:\TestFile.txt"
Set ObjFso = CreateObject("Scripting.FileSystemObject")

'Opening the file in READ mode
Set ObjFile = ObjFso.OpenTextFile(StrFileName)

'Reading from the file
StrData = ObjFile.Read(30)
WScript.Echo("The first 30 characters of the file is: " & StrData)

'Reading from the file
StrData = ObjFile.Read(30)
WScript.Echo("The next 30 characters of the file is: " & StrData)

'Closing the file
ObjFile.Close

While reading a specified number of bytes from a file, each newline character will be considered as two bytes. This is because in windows operating systems, a newline character is a combination of two characters; the 'Carriage Return' character (ASCII Code 13) and the 'Line Feed' character (ASCII Code 10).

Reading Each Line From the File (Using the 'ReadLine' Function)

Dim ObjFso
Dim StrFileName
Dim ObjFile
Dim StrData

StrFileName = "C:\TestFile.txt"
Set ObjFso = CreateObject("Scripting.FileSystemObject")

'Opening the file in READ mode
Set ObjFile = ObjFso.OpenTextFile(StrFileName)

'Reading from the file
StrData = ObjFile.ReadLine
WScript.Echo("The first line of the file is: " & StrData)

'Reading from the file
StrData = ObjFile.ReadLine
WScript.Echo("The next line of the file is: " & StrData)

'Closing the file
ObjFile.Close

While reading line by line from the file, the end of the line is identified by the newline character (combination of 'Carriage Return' and 'Line Feed').

Reading the Entire Contents of the File At Once (Using the 'ReadAll' Function)

Dim ObjFso
Dim StrFileName
Dim ObjFile
Dim StrData

StrFileName = "C:\TestFile.txt"
Set ObjFso = CreateObject("Scripting.FileSystemObject")

'Opening the file in READ mode
Set ObjFile = ObjFso.OpenTextFile(StrFileName)

'Reading from the file
StrData = ObjFile.ReadAll
WScript.Echo("The entire content of the file is: " & StrData)

'Closing the file
ObjFile.Close

Skip a Specified Number of Characters or Lines While Reading the File (Using the 'Skip' and 'SkipLine' Functions )

While reading from a file, you can skip a specified number of characters. The 'Skip' function can be used for this. This function takes an integer argument which is the number of characters to be skipped. There is one more function available in vbscript which is the 'SkipLine' function. This function skips the current line and move the pointer to the beginning of the next line. So if you are reading somewhere in the middle of a line and you want to skip the rest of the line you can use this function. The following piece of code demonstrates the use of both 'Skip' and 'SkipLine' functions. Before executing the following script make sure that the text file 'C:\TestFile.txt' has enough number of characters and lines.

Dim ObjFso
Dim StrFileName
Dim ObjFile
Dim StrData

StrFileName = "C:\TestFile.txt"
Set ObjFso = CreateObject("Scripting.FileSystemObject")

'Creating a file for writing data
Set ObjFile = ObjFso.OpenTextFile(StrFileName)

'Reading first 30 bytes (characters) from the file
StrData = ObjFile.Read(30)
WScript.Echo("The first 30 bytes is: " & StrData)

'Skiping next 25 characters
ObjFile.Skip(25)

'Reading the next 10 bytes (characters) after skipping
StrData = ObjFile.Read(10)
WScript.Echo("The next 10 bytes after skipping 25 bytes is: " & StrData)

'Skipping the current line
ObjFile.SkipLine

'Reading the next line after skipping
StrData = ObjFile.ReadLine
WScript.Echo("The next line after skipping is: " & StrData)

'Closing the file
ObjFile.Close

Check Whether We have Reached the End Of File (Using the 'AtEndOfStream' Function) (While using the 'Read' and 'ReadLine' Functions)

While reading from a file we can check whether we have reached the 'End of the File', using the function 'AtEndOfStream' defined in the 'FileStreamObject'. The function 'AtEndOfStream' will return boolean value. 'True' means the end of file is reached. 'False' means end of file is not reached.

Dim ObjFso
Dim StrFileName
Dim ObjFile
Dim StrData

StrFileName = "C:\TestFile.txt"
Set ObjFso = CreateObject("Scripting.FileSystemObject")

'Opening the file in READ mode
Set ObjFile = ObjFso.OpenTextFile(StrFileName)

'Checking for end of file
While ObjFile.AtEndOfStream = False
   'Reading from the file
   StrData = ObjFile.ReadLine
   WScript.Echo("The first/next line of the file is: " & StrData)
Wend

'Closing the file
ObjFile.Close

How the 'FileSystemObject' Identifies the End Of a File

As described in the 'FileSystemObject' section, the file system object is for the use of Text files only. It cannot open a file in binary mode. It identifies the End Of File using either the length of the file or the End Of File character (ASCII Code 26). So while reading from a file, if it sees the character 26 it will say that the end of file is reached ('AtEndOfStream' will return 'True'), even though the end of the file is not reached. Vbscript is unable to handle this deficiency. (In normal text files character 26 usually will not occur)

Powered by Bullraider.com