The simplest method is to use the FileSystemObject object. It is not a built-in VB object,
It can be used only after being referenced.
1. FileSystemObject Object Reference
"Project/reference/Microsoft startup runtime"
In the Object Browser window, select the "scripting" module. As you can see, many objects are added,
Drive FileSystemObject textstream File
Among them, FileSystemObject is the key to these objects and wants to use other objects,
You must first create a FileSystemObject object.
2. File Access
Dim FS as new FileSystemObject create FileSystemObject object
Dim txtf as textstream defines a textstream object variable
Textstrem objects and files must be opened before they can be read and written,
The filesystemobjet object provides two methods to open a file:
Opentextfile
Createtextfile: create a file
The file opened through FileSystemObject is a textstream object,
The textstream object can further read and write files through various methods and attributes.
1) opentextfile method: open a file
Set textstream object name = FileSystemObject name. opentextfile (file name, Io mode, whether to automatically create a file)
File Name: it is recommended to write the complete path file name.
Io mode: it can be set to forreading (= 1), forwrting (= 2), or forappending (= 8 ).
If it is set to forreading, The opened file is read-only;
If it is set to forwriting, The opened file is writable, and the content of the original file is cleared;
If it is set to forappending, The opened file is writable, but the content of the original file is not cleared, and data is written from the end of the file.
Default parameter: forreading read-only.
Whether to create files automatically: Set to true or false.
If it is set to true, a new file is automatically created when the file does not exist.
If it is set to false, an error occurs when the file does not exist.
Time-saving: false.
For example, open C:/autoexec. BAT to a read-only file.
Dim FS as new FileSystemObject
Dim txtf as textstream
Set txtf = FS. opentextfile ("C:/autoexec. Bat ")
For example, open C:/text.txt to "file written from the end of the file"
Dim FS as new FileSystemObject
Dim txtf as textstream
Set txtf = FS. opentextfile ("C:/test.txt", forappending, true)
Txtf. Close
2) createtextfile method: create a file
Set textstream object name = FileSystemObject name. createtextfile (file name, whether to overwrite the original file)
File Name: it is recommended to write the complete path file name.
Overwrite original file: Set to true or false. It can be set to the default value. The default value is true, which overwrites the original file.
If it is set to true, when the file exists, the original file will be destroyed and replaced by the new file.
If this parameter is set to false, an error occurs when the file exists.
For example, create a C:/text.txt file. If C:/test.txt exists, overwrite it.
Dim FS as new FileSystemObject
Dim txtf as textstream
Set txtf = FS. createtextfile ("C:/text.txt ")
3) fileexists method: Does the file exist?
To avoid damage to existing files, the parameter "Overwrite original file" is usually set to false. However, errors may occur. To avoid errors, You can first determine whether the file exists. In this case, call the fileexists method of the filesystemobjet object. The specific procedure is as follows:
'Fs is FileSystemObject
If fs. fileexists ("C:/test.txt") then' file already exists
'Other Processing Methods
Else
Set txtf = FS. createtextfile ("C:/text.txt ")
End if
4) Readline and writeline Methods: read and write files
Using createdtextfile and opentextfile, a text file is opened (created). to access a text file, it is usually read row by row or written row by row. The Readline method of the textstream object must be called during reading. You must call the writeline method when writing data.
Example:
'Txtf and txtf2 are both textstream objects.
S = txtf. Readline reads a row of data and sets it to the S variable.
Txtf2.writeline s' writes data in variable s as a file row
5) atendofstream attribute: has the file reached the end?
When the reading position has reached the end of the file, if you call readlin to read data, an error will be generated. To avoid this error, you must determine the atendofstream attribute of the textstream object. If it is true, it indicates that the file location has reached the end of the file and data cannot be read.
If not txtf. atendofstream then
S = txtf. Readline
Endif
For example, copy the C:/autoexec. BAT file to the C:/autoexec. Bak file.
Dim FS as new FileSystemObject
Dim txtf1 as textstream
Dim txtf2 as textstream
Dim s as string
Set txtf1 = FS. opentextfile ("C:/autoexec. Bat", forreading, true)
Set txtf2 = FS. createtextfile ("C:/autoexec. Bak", true)
While not txtf1.atendofstream 'If txtf1 has not reached the end of the file
S = txtf1.readline' read a row of data from textf1
Txtf2.writeline s' writes data to a file as a row of txtf2
Wend
Txtf1.close
Txtf2.close
6) readall method: read all the content of a file at a time.
The Readline method reads only one row of data at a time. The readall method reads all the content of a file at a time.
'Txtf is a textstream object
If not txtf. atendofstream then
Text1.text = txtf. readall
End if
End sub
7) read and write Methods: Reading and Writing files
The biggest difference between the read and Readline methods is that Readline reads a row of data each time, but the read method reads n characters each time. For example:
N = 100
S = txtf. Read (n) 'reads 100 characters
If the number of characters in the file is less than 100, assuming that only 50 characters are used, the read operation reads all 50 characters. In addition to reading methods, Readline can only be used to read style files. The read method can also be used to read binary files. (For example,. Ext, DLL, BMP, and other format files)
The biggest difference between the write and writeline methods is that the writeline writes more CHR (13) and CHR (10) characters to the file each time it writes data to the file, however, write only writes data.
For example, use the readall and write methods to copy the C:/autoexec. BAT file to the C:/autoexec. Bak file.
Dim FS as new FileSystemObject
Dim txtf as textstream, txtf2 as textstream
Set txtf = FS. opentextfile ("C:/autoexec. Bat", forreading, true)
Set txtf2 = FS. createtextfile ("C:/autoexec. Bak", true)
Txtf2.write txtf. readall
Txtf2.close
Txtf. Close
First, define a global variable textchange to indicate whether the text box has been modified.
2 In the new_click () event
Private sub new_click ()
If textchange = 1 then
AA = msgbox ("whether to save modified files", vbokcancel)
If AA = 1 then
SaveFile 'user-defined process, used to save files
End if
End if
Text1.text = ""
Text1.setfocus
Textchange = 0
End sub
SaveFile process:
Private sub SaveFile ()
Dim FS as new FileSystemObject
Dim txtf as textstream
Commondialog1.showsave
Set txtf = FS. opentextfile (commondialog1.filename, forwriting, true)
Txtf. Write text1.text
Txtf. Close
Textchang = 0
End sub
3. In the open_click () event
Private sub open_click ()
Dim FS as new FileSystemObject
Dim txtf as textstream
If textchange = 1 then
AA = msgbox ("do you want to save the existing changes? ", Vbokcancel)
If AA = 1 then
SaveFile
End if
End if
Text1.text = ""
Textchange = 0
Commondialog1.showopen
Set txtf = FS. opentextfile (commondialog1.filename, forreading, true)
Text1.text = txtf. readall
Txtf. Close
End sub
Private sub save_click ()
SaveFile
End sub
Private sub toolbar1_buttonclick (byval button as mscomctllib. button)
Select case button. Key
Case "open"
Open_click
Case "new"
New_click
Case "save"
Save_click
Case "copy"
Mcopy_click
Case "cut"
Mcut_click
Case "Paste"
Mpaste_click
End select
End sub