Home Blogs Library Forums Support Advertise
VB Script
 
A Basic Tutorial
File System Programing
 
VBScript and MS Excel
VBScript and MS Access
 
VBScript and MS Word
VBScript and MS Powerpoint
 
Windows Registry Programing
 
Windows Administration
 
Active Directory Programing
 
Miscellaneous Functions
 
You are here...
Home > Vb Script - Home > File System programing > Opening an Existing File

VBScript - Opening an Existing File

This section explains how to open a text file for reading, writing or appending using vbscript. 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, of the file to be opened.

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 the file for READ
2 Open the file for WRITE
8 Open the 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 hexadecimal error code is '800A0035'. 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 a UNICODE file.
0 Open the file as ASCII file.
-2 Open the file with the 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