When developing WEB applications, we often need to process drives, folders and files in the file system, such as collecting drive-related information; creating, adding, moving or deleting folders and files, etc. In VB6, a new object model called FSO (File System Object) is provided to access the file system. This model provides an object-based tool. Through a series of properties and methods it provides, we can perform various operations on the file system more simply and flexibly in the application.
1. Introduction to FSO The FSO object model includes the following objects:
Drive object: allows the collection of information such as available space and share names of drives such as hard disks and CD-ROMs that are physically connected to the system or are logically connected to the system through LAN.
Folder object: allows you to create, delete or move folders, and query the system for the name, path, etc. of the folder.
Files object: allows you to create, delete or move files, and query the system for file names, paths, etc.
TextStream object: allows the creation, reading and writing of text files.
FileSystemObject object: Provides a complete set of methods for drive, folder and file operations. Functionally, it can be regarded as a collection of the above objects and is often used in conjunction with them. Many of the methods associated with this object duplicate those in the previous four objects, so we can perform most operations on drives, folders, and files either through the FileSystemObject object or through the corresponding drive, folder, or file object. Operate these components. The FSO model implements operations on the same object through two methods, and the operation effects are the same. The purpose of providing this redundant function is to achieve maximum programming flexibility.
In this article, we will explain the operation of text files using the TextStream object of the FSO object model.
(1) Use FileSystemObject to obtain text file objects 1. Create a FileSystemObject object instance To perform file operations, you must first create a FileSystemObject object instance to create or open a file. The specific format for creating a FileSystemObject object instance is (taking AFileSystemObject) as an example:
Set AFileSystemObject = CreateObject("Scripting.FileSystemObject")
2. Use FileSystemObject to obtain the text file object TextStream
FileSystemObject provides two methods for obtaining text file objects TextStream, among which CreateTextFile is used to create files, and OpenTextFile is used to open existing files. The return result of both methods is an instance of TextStream object. Use this object Specific operations on files can be performed.
⑴Create 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)
in:
NewFileName is a string value that specifies the name of the file to be created, usually the actual path of the file plus the file name, such as C:webshareaspsampfiletest.txt
OverwriteExistingFile is a Boolean value indicating whether to overwrite the original file if a file with the same name exists. This parameter can be omitted, and the default is False, which means the original file will not be overwritten.
IsUnicode is a Boolean value indicating whether 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.
⑵ Open an existing file. The specific format of the method of opening an existing file is (take AFileSystemObject as an example):
AFilesystemObject.OpenTextFile(FileName,IOMode,create,format)
in:
FileName is a string value that specifies the name of the file to be opened, usually the actual path of the file plus the file name, C:filepathtest.txt
IOMode is a constant value, indicating the purpose of opening the file, and ForReading(1) means reading data;
ForAppending means used to add data. This parameter can be omitted, and the default is ForReading.
Create is a Boolean value indicating whether to create a new file when the file to be opened does not exist.
This parameter can be omitted, and the default is False, which means no new file will be created.
Format represents the way the file is opened. Its 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, and the default is TristateFalse, which is ASCII mode.
(two). Using TextStream for File Operations After creating or opening a file, you can use the methods provided by the object TextStream to perform actual file operations.
1. The methods used for write operations are:
⑴Write(string)
Writes the string specified by string to the file.
⑵WriteLine(string)
Writes the string specified by string to the file, and writes a newline character.
The parameter string can be omitted, in which case a blank line will be inserted into the file.
⑶WriteBlankLines(NumOfLines)
Insert a number of blank lines into the file, the number of lines is specified by NumOfLines.
2. The methods and attribute methods used for read operations are:
⑴AtEndOfLine
This attribute is a Boolean value indicating whether the file pointer points to the end of the current line.
⑵AtEndOfStream
This attribute is a Boolean value indicating whether the file pointer points to the end of the file.
⑶ Column
This attribute is an integer value representing the position of the file pointer in the current line.
⑷Line
This attribute is an integer value representing the line number of the line where the file pointer is located.
⑸ Read(NumOfCharacters)
This method starts from the current position of the file, reads in a number of characters specified by the number of NumOfCharacters, and returns a string.
⑹ReadLine
This method starts from 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 from the current position, reads the contents of the entire file until the end of the file, and returns a string.
⑻ Skip(NumOfCharacters)
This method starts from the current position of the file and skips a number of characters specified by the NumOfCharacters number.
⑼ SKipLine
This method starts from the current position of the file and skips the contents of the current line.
3. The methods used to close files are:
⑴ Close
Close already created or opened files.
(3) The following is an example to illustrate how to use FSO to read text files and save them to the 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="40">
<INPUT TYPE="submit" name ="dr" value="Import information">
</div>
</FORM>
…
2. Write the code to save the obtained text value to the database: upFile.asp
<%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" 'Call the stored procedure
ObjComm.CommandType=adCmdStoredProc
Set ObjComm.ActiveConnection=Conn
''''''''''Create input and output parameters''' ''''''''''''''
Set ObjParamECom=ObjComm.CreateParameter("WC_ECompanyName",adVarchar,adParamInput,100)
ObjComm.Parameters.Append ObjParamECom
'@in_ECompanyName Varchar(50), --Company English name
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
...
'''''''''' Parameter creation completed'''''''''''''''' '''
%>
<%
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 WHILE NOT ATextStream.AtEndOfStream
''''''Initialize variables''''''''''''''
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
''''''''''Add the variable to the parameter set'''''''' '''''
ObjParamECom.value=strLine1
ObjParamCCom.value=strLine2
ObjParamAddr.value=strLine3
...
''''''''''Operation ends'''''''''''''''' '''
ObjComm.Execute() 'Run command
LOOP
response.write "<br>"+"Import library successfully! <a href=dolist.html>[Continue import]</a><br>"
set Conn=nothing
set FSO=nothing
set ATextStream=nothing
%>
Attachment: Stored procedure sp_AddMsg code
CREATE PROCEDURE dbo.sp_AddMsg -- Import foreign enterprises Information
(
@in_CompanyName Varchar(100), --Company name
@in_Address Varchar(200), --Company address
@in_City Varchar(100), --Company city
...
)
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 explanation is over, I hope this article The article can bring some help to readers.