In developing web applications, we often need to process drives, folders, and files in the file system, such as collecting information about the drive; create, add, move, or delete folders and files, etc. A new set of FSO (File System Object) object models is available in VB6 to access the file system. This model provides an object-based tool that makes a variety of operations in the application through a series of properties and methods provided by it.
I. Introduction to FSO
The FSO object model contains the following objects:
Drive Object: Allows collection system physical or via LAN and system logical connections, CD-ROM and other drives, shared names and other information.
Folder object: Allow creation, deleting, or moving folders, and querying the name, path of the folder, etc. to the system.
Files object: Allow creation, delete, or moving files, and query the name, path of the file, etc.
TEXTSTREAM object: Allows creation and reading and writing text files.
FileSystemObject Object: Provides a single way for drives, folders, and file operations, which can be used as a collection of several objects above and often use them. Many methods associated with this object repeat the methods in the previous four objects, so we can do most of the drives, folders, and files through the FileSystemObject object, or through the corresponding drive, folder or file object. These components are operated. The FSO model implements the operation of the same object by two ways, and its operational effect is the same, and the purpose of providing this redundancy function is to achieve maximum programming flexibility.
In this article, we will explain the TEXTSTREAM objects that use the FSO object model to the operation of the text file.
(1) Use FileSystemObject to get text file objects
1. Create an instance of a FileSystemObject object
To perform file operations, you must first create a FileSystemObject object instance to create or open a file. Creating a FileSystemObject object instance instance is (in AfileSystemObject) as an Example:
SET AFILESYSTEMOBJECT = CREATEOBJECT ("scripting.filesystemobject")
2. Use FileSystemObject to get text file object TextStream
FileSystemObject provides two ways to get text file object TextStream, which is used to create
The file is CreateTextFile, which is used to open the existing file is OpenTextFile, and the return result of the two methods is
An instance of a TextStream object that utilizes this object.
(1) Creating a new file
The specific format of the method of creating a new file is (take AfileSystemObject as an example):
AfileSystemObject.createtextFile (NewFileName, Overwriteexistingfile, isunicode)
among them:
NewFileName is a string value that specifies the name of the file to be created, usually the actual path of the file.
Add a word name, such as c: /webshare/aspsamp/filetest.txt
OverWriteExistingFile is a boolean value, indicating whether it is overwritten if there is a file existence.
The original document. This parameter can be omitted, and the default is false, ie the original file is not overwritten.
Isunicode is a Boolean value that means that the file to be created is an ASCII file or a Unicode file. This parameter can be omitted, and the default is false, which is an ASCII file.
(2) Open existing files
The specific format of the method that has existing files is (take AfileSystemObject as an example):
AfileSystemObject.opentextFile (FileName, Iomode, Create, Format)
among them:
FileName is a string value that specifies the name of the file to be opened, usually the actual path of the file.
Add a word name, c: /filepath/test.txt
Iomode is a constant value, indicating that the purpose of opening the file, forreading (1) means for reading data;
FORAPPPpending means for adding data. This parameter can be omitted, and it is forreading by default.
CREATE is a boolean value, indicating whether a new file is created when the file to be opened does not exist.
This parameter can be omitted, and the default is false, that is, no new files are created.
Format indicates the way the file is opened. Its possible value and meanings are as follows:
Tristatetrue: Open in Unicode.
Tristatefalse: Open in ASCII.
TristateuseDefault: Open by the default manner.
This parameter can be omitted, the default is TristateFalse, which is an ASCII mode.
(two). File operation with TextStream
After establishing or opening the file, the method of files can be used for files using the methods provided by the object TextStream.
1. Methods for writing operations are:
(1) Write (String)
Write the string specified by String to the file.
(2) WriteLine (String)
Write a string specified by String and written in a file and write a wrap character.
Parameters String can be omitted, and an empty line will be inserted in the file.
(3) Writeblanklines (Numoflines)
Insert a number of spaces in the file, the number of rows is specified by numoflines.
2. Methods and attribute methods for reading operations are:
(1) ATENDOFLINE
This attribute is a boolean value indicating whether the file pointer has pointed to the current row.
(2) ATENDOFSTREAM
This property is a Boolean value indicating whether the file pointer has pointed to the file.
(3) Column
This property is an integer value indicating the location of the file pointer in the current row.
⑷ LINE
This attribute is an integer value indicating the line number of the row where the file pointer is located.
⑸ Read (NumOfcharacters)
This method starts from the current location of the file, reads a number of characters specified by the number of numoftcharacters, returns one
String.
⑹ Readline
The method starts from the current location of the file, read into the content of the current line until the end, returns a string.
⑺ Readall
The method starts from the current location, read into the content of the entire file until the file ends, returns a string.
⑻ Skip (NumOfcharacters)
The method starts from the current location of the file, skips several characters specified by the number of numoftcharacters.
⑼ Skipline
This method begins with the current location of the file and skip the content of the current line.
3. Methods for shutting down files are:
(1) Close
Close the file that has been established or opened.
(3), below illustrate how to use FSO to read text files and how to save to the database:
1. Create a page that reads the file path: file.htm
...