Import text information into the database using FSO
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. 1. FSO Introduction The FSO object model contains the following objects: Drive Object: Allows the system physical or through the LAN and the system logical, the available space, shared name, etc. of the CD-ROM and other drives. 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. Creating a FileSystemObject object instance To perform a file action, you must first create a FileSystemObject object instance to create or open a file. Creating a FileSystemObject object instance The specific format is (in AfileSystemObject) as an example: set AfileSystemObject = CreateObject ("scripting.filesystemObject") 2. Using FileSystemObject to get text file objects TextStreamFileSystemObject provides two ways to get text file object TextStream, where creating files are cretextFile, used to open an existing file is OpenTextFile, two methods return results are a TextStream An example of an object, using this object to perform a specific operation of the file. (1) Creating a new file Creating a new file for the specific format (take AfileSystemObject as an example): AfileSystemObject.createtextFile (NewFileName, Overwriteexistingfile, isunicate) where: NewFileName is a string value, specify the name of the file to be created, usually a file The actual path add file name, such as c: /webshare/aspsamp/filetest.txt overwriteexistingfile is a boolean value, indicating whether or not to overwrite the original file if there is a file existence. 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 the existing file to open the specific format of the presented file, as an example of AfileSystemObject: AfileSystemObject.OpenTextFile (FileName, Iomode, Create, Format) where: FileName is a string value, specify the name of the file you want to open , Usually the actual path of the file is added, c: /filepath/test.txt omode is a constant value, indicating the purpose of opening the file, forreading (1) means for reading data; FORAPPpending means for increasing data. This parameter can be omitted, and it is forreading by default. Create is a Boolean value that indicates whether the file to be opened does not exist, the parameter can be omitted, and the default is false, that is, the new file is not created. Format indicates the way the file is opened. Its possible values 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 actual operation of the file can be performed using the method provided by the object TextStream. 1. Methods for write operations include: (1) Write (string) Writes to the file by string specified by String. (2) WriteLine (string) writes the string specified by the string in the file and writes a wrap character. Parameters String can be omitted, and an empty line will be inserted in the file. (3) WriteBLANKLines (Numoflines) Inserts several spaces in the file, and the number of rows is specified by numoflines. 2. Methods and attribute methods for reading operations are: (1) ATENDOFLINE This property is a boolean value that indicates whether the file pointer has pointed to the current line. (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 property 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 string of characters specified by NumOfcharacters, returns a string. ⑹ Readline This method starts from the current location of the file, read into the content of the current row until the end, returns a string. ⑺ Readall This method starts from the current location, read the content of the entire file until the file ends, returns a string. ⑻ Skip (NumOfcharacters) This method starts from the current location of the file, skips several characters specified by the number of NumOfcharacters. ⑼ Skipline This method starts from 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
...