Use FSO to import text information into a database

Source: Internet
Author: User
Tags file system integer new set object model readline trim
fso| Import Data | database


In developing Web applications, we often need to process drives, folders, and files in a file system, such as collecting information about drives, creating, adding, moving, or deleting folders and files. A new set of the FSO (File system object) object model is provided in VB6 to access processing of the filesystem. The model provides an object-based tool that enables us to perform various operations on the file system more easily and flexibly in the application through a series of properties and methods provided by it.
First, FSO Introduction
The FSO object model consists of the following kinds of objects:
Drive object: Allows you to collect information such as the free space, share name, and so on of the hard disk, CD-ROM, and so on that the system is physically or connected to the system via LAN.
Folder object: Allows you to create, delete, or move folders, and query the system for the name, path, and so on.
Files object: Allows you to create, delete, or move a file, and query the system for the file name, path, and so on.
TextStream object: Allows you to create and read and write text files.
FileSystemObject object: Provides a complete set of methods for drive, folder, and file operations, which can be viewed functionally as a collection of the above objects and are often used in conjunction with them. Many of the methods associated with this object repeat the methods in the previous four objects, so we can do most of the work on the drives, folders, and files through the FileSystemObject object, or we can manipulate the components through the corresponding drive, folder, or file object. The FSO model implements operations on the same object in two ways, with the same effect, providing this redundancy to maximize programming flexibility.

In this article, we will explain the operation of a text file using the TextStream object model of the FSO object.

(i) Use FileSystemObject to get text file objects
1. Create a FileSystemObject object instance
To do a file operation, you must first create a FileSystemObject object instance to create or open a file. The specific format for creating a FileSystemObject object instance is (in Afilesystemobject) as an example:
Set Afilesystemobject = CreateObject ("Scripting.filesystemobjecct")
2. Use FileSystemObject to get text file objects TextStream
FileSystemObject provides two ways to get the text file object TextStream, which is used to create
The file is CreateTextFile, which is used to open the existing file is OpenTextFile, both methods return the result is
An instance of an TextStream object that can be used to manipulate files.
⑴ Create a new file
The exact format of the method to create the new file is (in Afilesystemobject):
Afilesystemobject.createtextfile (Newfilename,overwriteexistingfile,isunicode)
which
NewFileName is a string value that specifies the name of the file to be established, usually the actual path to the file
Add file name, such as C:\webshare\aspsamp\filetest.txt
Overwriteexistingfile is a Boolean value that indicates whether to overwrite if a file with the same name exists
The original file. This argument can be omitted, and the default is false, that is, the original file is not overwritten.
Isunicode is a Boolean value that indicates whether the file to be created is an ASCII file or a Unicode file.
This argument can be omitted, and the default is False, which is the ASCII file.
⑵ open a file that already exists
The exact format of the method to open a file that already exists is (in Afilesystemobject, for example):
Afilesystemobject.opentextfile (Filename,iomode,create,format)
which
FileName is a string value that specifies the name of the file to open, usually the actual path to the file
Add file name, C:\filepath\test.txt
IOMode is a constant value indicating the purpose of opening the file, ForReading (1) is used to read the data;
ForAppending is used to increase data. This parameter can be omitted, and the default is ForReading.
Create is a Boolean value that indicates whether a new file is created when the file you want to open does not exist.
This argument can be omitted, and the default is false, that is, no new file is created.
The format indicates how the file is opened. The possible values and meanings are as follows:
TristateTrue: Open in Unicode mode.
Tristatefalse: Open in ASCII mode.
Tristateusedefault: Open in the system default mode.
This parameter can be omitted, the default is Tristatefalse, that is, the ASCII method.
(b). Using TextStream for file operations
After you create or open a file, you can use the method provided by the object TextStream to do the actual operation of the file.
1. The methods used for write operations are:
⑴write (String)
Writes the string specified by string to the file.
⑵writeline (String)
Writes a string specified by string and writes a newline character in the file.
The parameter string can be omitted, and a blank line is inserted in the file.
⑶writeblanklines (Numoflines)
Inserts several blank rows in the file, and the number of rows is specified by Numoflines.
2. Methods and property methods for read operations are:
⑴atendofline
This property is a Boolean value that indicates whether the file pointer has pointed to the end of the line at the current line.
⑵atendofstream
This property is a Boolean value indicating whether the file pointer has pointed to the end of the file.
⑶column
This property is an integer value that represents the position of the file pointer in the current row.
⑷line
This property is an integer value that represents the line number of the row that contains the file pointer.
⑸read (Numofcharacters)
This method starts at the current position of the file, reads a number of characters specified by the number of numofcharacters, and returns a
String
⑹readline
This method starts at the current position of the file, reads the contents of the current line until the end of the line, and returns a string.
⑺readall
This method starts at the current position and reads the contents of the entire file until the end of the file, returning a string.
⑻skip (Numofcharacters)
This method starts at the current position of the file and skips several characters specified by the number of numofcharacters.
⑼skipline
This method skips the contents of the current row, starting at the current position of the file.
3. The methods used to close the file are:
⑴close
Closes a file that has been created or opened.

(iii) The following is an example of how to use the FSO to read text files and how to save to a database:

1. First create a page to read the file path: file.htm

...

<form method=post action= "upfile.asp" >

<div align= "center" > <br>

<br>

<br>

<br>

<input type= "file" name= "path" size= ">"

<input type= "Submit" Name= "Dr" value= "Import Info" >

</div>

</FORM>

...

2, write to get the text value saved to the database code: upfile.asp

<% @Language = "VBScript"%>

<%Response.Buffer=true%>

<!--#include file= "Adovbs.inc"-->

<%

Strconn= "Dsn=datasourcename"

Set Conn=server.createobject ("ADODB. Connection ")

Conn.Open strconn

Set Objcomm=server.createobject ("Adodb.command")

objcomm.commandtext= "sp_addmsg" Calls stored procedures

Objcomm.commandtype=adcmdstoredproc

Set Objcomm.activeconnection=conn

' To create an input or output parameter ', ' ' ' ' ' ' ' ' ' '

Set objparamecom=objcomm.createparameter ("Wc_ecompanyname", advarchar,adparaminput,100)

ObjComm.Parameters.Append objparamecom

' @in_ECompanyName Varchar (50),--English name of the company

Set objparamaddr=objcomm.createparameter ("wc_address", advarchar,adparaminput,200)

ObjComm.Parameters.Append objparamaddr

' @in_Address Varchar (50),--company address

Set objparamcity=objcomm.createparameter ("wc_city", advarchar,adparaminput,100)

ObjComm.Parameters.Append objparamcity

' @in_City Varchar (50),--City

...

' ' Create a parameter complete ', ' ' ' ', ' ' ' ' ' ' ' '

%>

<%

Dim Alltext,strline1,strline2,strline3

Dim Strpath,fileurl

Fileurl= ""

Strpath=trim (Request.Form ("path"))

Fileurl=strpath

SET fso=createobject ("Scripting.FileSystemObject")

SET Atextstream=fso. OpenTextFile (Fileurl,1,false,tristatefalse)

' ' Extract data ', ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '

Do as not Atextstream.atendofstream

' ' Initialize variable ' "' '" ' "'" ' "

Strline1= ""

Strline2= ""

Strline3= ""

...

'''''''''''''''''''''''''''''''

Atextstream.skipline

Atextstream.skip (11)

Strline1=trim (Atextstream.readline)

Atextstream.skip (11)

Strline2=trim (Atextstream.readline)

Atextstream.skip (5)

Strline3=trim (Atextstream.readline)

...

' End If

"" To "" "" "" "" "" "" "" "

Objparamecom.value=strline1

Objparamccom.value=strline2

Objparamaddr.value=strline3

...

"" "" "" "" "" "" "" "" "" "" "

Objcomm.execute () ' Run command

LOOP

Response.Write "<br> +" Import library success! <a href=dolist.html>[continue to import]</a><br> "

Set conn=nothing

Set fso=nothing

Set atextstream=nothing

%>

Attached: Stored procedure sp_addmsg code

CREATE PROCEDURE dbo.sp_addmsg--Import foreign enterprise information

(

@in_CompanyName Varchar (100),--Company name

@in_Address Varchar (200),--company address

@in_City Varchar (100), the city where the company is located

...

)

As

SET NOCOUNT on

BEGIN TRAN

INSERT into Tb_wclibrary (

Wc_companyname,

Wc_ccompanyname,

Wc_address,

...

)

VALUES (

@in_CompanyName,

@in_CCompanyName,

@in_Address,

...

)

IF @ @ERROR <> 0

BEGIN

ROLLBACK TRAN

RETURN-1

End

COMMIT TRAN

return 0

SET NOCOUNT off

At this point, the full text of the end, I hope this article can bring some help to readers.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.