Open an Existing Text File in VB Script

In vb script, a file can be opened using the 'OpenTextFile' function of the file system object. This function will return a 'TextStreamObject' which can be used to read/write data from/into the file. The syntax of the 'OpenTextFile' function is given below 

Syntax: OpenTextFile(filename [, iomode[, create[, format]]])

The argument 'filename' is a mandatory argument. We need to give the filename along with the complete path. 

The argument 'iomode' is an optional argument. It specifies the mode in which the file to be opened. This is an integer argument that accepts 1, 2 or 8. The following table shows the meaning of each values. The default value is 1 (READ)

1 Open File for READ
2 Open File for WRITE
8 Open File for APPEND

 

 

 

The argument 'create' is also an optional boolean argument. It can take either 'True' or 'False'. 'True' menas if the specified file doesn't exists, a new file with that name will be created. 'False' means if the specified file is not there vbscript will return an error. The default value is 'False'. 

The argument 'format' is an integer argument. It can accept -1, 0 or -2. The following table shows the meaning of each value. The default value is -2 (Default System Setting)

-1 Open the File as UNICODE File
0 Open the File as ASCII File
1 Open the File with Default System Setting

 

 

 

Open a Text File for READ

Dim ObjFso
Dim StrFileName
Dim ObjFile

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

If ObjFso.FileExists(StrFileName) Then
   Set ObjFile = ObjFso.OpenTextFile (StrFileName)
   WScript.Echo("The Content of the file is: " & ObjFile.ReadAll)
   ObjFile.Close
Else
   WScript.Echo("The specified file is not found!!")
End If

Open a Text File for WRITE (If the File doesn't Exist, a New File will be Created) (If the File Exists and has some data, It will be overwritten)

Dim ObjFso
Dim StrFileName
Dim ObjFile

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

'Opening the file
Set ObjFile = ObjFso.OpenTextFile (StrFileName, 2, True)

'Writing some data into the file
ObjFile.WriteLine("This is some sample data.")

'Closing the file
ObjFile.Close

Open a Text File for APPEND (If the File Exists It Will be Appended) (If the File doesn't Exists, A New File will be Created.)

Dim ObjFso
Dim StrFileName
Dim ObjFile

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

'Opening the file
Set ObjFile = ObjFso.OpenTextFile (StrFileName, 8, True)

'Writing some data into the file
ObjFile.WriteLine("This is some sample data.")

'Closing the file
ObjFile.Close

Open a Text File for APPEND in UNICODE format (If the File Exists It Will be Appended) (If the File doesn't Exists, A New File will be Created.)

Dim ObjFso
Dim StrFileName
Dim ObjFile

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

'Opening the file
Set ObjFile = ObjFso.OpenTextFile (StrFileName, 8, True, -1)

'Writing some data into the file
ObjFile.WriteLine("This is some sample data.")

'Closing the file
ObjFile.Close
Powered by Bullraider.com