You can work with files in Visual Basic by using the new object-oriented FSO objects such as Copy,
A D V E R T I S E M E N T
Delete, Move, and OpenAsTextStream, among others, or by using the older existing functions such as Open, Close, FileCopy, GetAttr, and so forth. Note that you can move, copy, or delete files regardless of their file type.
Creating files
To create a file , use the following code
There are three ways to create a sequential text file (sometimes referred to as a "text stream"). One way is to use the CreateTextFile method. To create an empty text file:
Dim fso As New FileSystemObject, fil As File
Set fil = fso.CreateTextFile("c:\testfile.txt", True)
Note The FSO object model does not yet support the creation of random or binary files. To create random and binary files, use the Open command with either the Random or Binary flag. Full information on how to manipulate random and binary files is contained in "Using Random File Access" and "Using Binary File Access" in this chapter.
Another way is to use either the OpenTextFile method of the FileSystemObject object with the ForWriting flag set:
Dim fso As New FileSystemObject, ts As TextStream
Set ts = fso.OpenTextFile("c:\test.txt", ForWriting)
Or you can use the OpenAsTextStream method with the ForWriting flag set:
Dim fso As New FileSystemObject, fil As File, ts As TextStream
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("test1.txt")
Set fil = fso.GetFile("test1.txt")
Set ts = fil.OpenAsTextStream(ForWriting)
Each file created must have a file number and a file name for identification and as for file name, you must also specify the path where the file will reside.
For example
Open "c:\My Documents\sample.txt" For Output As #1
will create a text file by the name of sample.txt in the My Document folder and the accompany file number is 1. If you wish to save and create the file in A drive, simply change the path, as follows"
Open "A:\sample.txt" For Output As #1
If you wish to create a HTML file , simple change the extension to .html as follows:
Open "c:\My Documents\sample.html" For Output As # 2
The following program creates a text file:
Private Sub create_Click()
Dim intMsg As String
Dim StudentName As String
Open "c:\My Documents\sample.txt" For Output As #1
intMsg = MsgBox("File sample.txt opened")
StudentName = InputBox("Enter the student Name")
Print #1, StudentName
intMsg = MsgBox("Writing a" & StudentName & " to sample.txt ")
Close #1
intMsg = MsgBox("File sample.txt closed")
End Sub
The above program will create a file sample.txt in the My Documents' folder and ready to receive input from users and any data input by users will be saved in this text file.
Reading files
To read a file created in previous example, you can use the input # statement and we can only read the file according to the format when it was written. You have to open the file according to the variable that hold the data and its file number.
We also need to declare the variable using the DIM command only.
The following example is used to read a text file:
Private Sub Reading_Click()
Dim variable1 As String
Open "c:\My Documents\sample.txt" For Input As #1
Input #1, variable1
Text1.Text = variable1
Close #1
End Sub
This program will open the sample.txt file and display its contents in the Text1 textbox itself.