In 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 set of object models called FSO (File System Object) is provided to access the file system. The 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 contains the following types of objects:
Drive object: allows the collection of available space, shared names and other information of hard disks, CD-ROMs and other drives physically or through the system logic connected to the system through the system.
Folder object: allows creating, deleting or moving folders, and querying the system for the name, path, etc. of the folder.
Files object: allows the creation, deletion or movement of files, and query the system for the name, path, etc. of the file.
TextStream object: Allows the creation and reading and writing of text files.
FileSystemObject Object: Provides a complete set of methods for drive, folder and file operations. It can be regarded as a collection of the above objects and is often used with them. Many methods associated with this object repeat the methods in the previous four objects, so we can perform most operations on drives, folders, and files through the FileSystemObject object, or operate on these components through the corresponding drives, folders, or file objects. The FSO model implements operations on the same object through two methods, and its operational 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 model.
(I) 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("")
2. Use FileSystemObject to get text file object TextStream
FileSystemObject provides two methods for getting text file objects TextStream, which is used to create
The file is CreateTextFile, and the file is OpenTextFile. The result of both methods is
An instance of a TextStream object, using this object to perform specific files operations.
⑴ Create a new file
The specific format of the method to create a new file is (taking AFileSystemObject as an example):
(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.
Add file name, such as C:\webshare\aspsamp\
OverwriteExistingFile is a Boolean value indicating whether to overwrite if a file with the same name exists.
Original file. This parameter can be omitted, and by default it is False, that is, 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 by default it is False, that is, an ASCII file.
⑵ Open an existing file
The specific format of the method to open an existing file is (taking AFileSystemObject as an example):
(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.
Add file name, C:\filepath\
IOMode is a constant value, indicating the purpose of opening the file, and ForReading(1) indicates it is used to read data;
ForAppending means to increase data. This parameter can be omitted, and by default it is ForReading.
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 by default it is False, that is, no new file is created.
Format represents the way the file is opened. The possible values and meanings are as follows:
TristateTrue: Open in Unicode mode.
TristateFalse: Open as ASCII.
TristateUseDefault: Open in the default mode of the system.
This parameter can be omitted, and by default it is TristateFalse, that is, ASCII method.
(two). Use TextStream to perform file operations
After creating or opening the file, you can use the methods provided by the object TextStream to perform actual operation of the file.
1. The methods used for writing operations are:
⑴ Write(string)
Write the string specified by string to the file.
⑵ WriteLine(string)
Write a string specified by string in the file and write a newline character.
The parameter string can be omitted, and a blank line will be inserted into the file at this time.
⑶ WriteBlankLines(NumOfLines)
Insert several blank lines into the file, the number of lines is specified by NumOfLines.
2. The methods and attribute methods used for reading operations are:
⑴ AtEndOfLine
This property is a Boolean value indicating whether the file pointer has pointed to the end of 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 indicates the position of the file pointer in the current line.
⑷ Line
This property is an integer value that represents the line number of the line where the file pointer is located.
⑸ Read(NumOfCharacters)
This method starts from the current location of the file, reads in several characters specified by the number of NumOfCharacters, and returns a
String.
⑹ ReadLine
This method starts from the current location of the file, reads the content of the current line until the end of the line, and returns a string.
⑺ ReadAll
This method starts from the current location, 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 location of the file and skips several characters specified by the number of NumOfCharacters.
⑼ SKipLine
This method starts from the current location of the file and skips the content of the current line.
3. The methods used to close files are:
⑴ Close
Close the created or opened file.
(3) Here is an example to illustrate how to use FSO to read text files and save them to the database:
1. Create a page that reads the file path first:
…
<FORM METHOD=POST ACTION="" >
<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 obtained text value to the database code:
<%@Language="VBScript"%>
<%=true%>
<!--#include file=""-->
<%
strConn="DSN=DataSourceName"
set Conn=("")
strConn
set ObjComm=("")
="sp_AddMsg" 'Call stored procedure
=adCmdStoredProc
Set =Conn
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set ObjParamECom=("WC_ECompanyName",adVarchar,adParamInput,100)
ObjParamECom
'@in_ECompanyName Varchar(50), --Company English name
Set ObjParamAddr=("WC_Address",adVarchar,adParamInput,200)
ObjParamAddr
'@in_Address Varchar(50), --Company Address
Set ObjParamCity=("WC_City",adVarchar,adParamInput,100)
ObjParamCity
'@in_City Varchar(50), --City
…
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
%>
<%
dim AllText,strLine1,strLine2,strLine3
dim strpath,fileurl
fileurl=""
strpath=Trim(("path"))
fileurl=strpath
SET FSO=CreateObject("")
SET ATextStream=(fileurl,1,false,TristateFalse)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
DO WHILE NOT
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
strLine1=""
strLine2=""
strLine3=""
…
'''''''''''''''''''''''''''''''
(11)
strLine1=Trim()
(11)
strLine2=Trim()
(5)
strLine3=Trim()
…
'End if
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
=strLine1
=strLine2
=strLine3
…
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
() 'Run command
LOOP
"<br>"+"Import the library successfully! <a href=>[Continue to import]</a><br>"
set Conn=nothing
set FSO=nothing
set ATextStream=nothing
%>
Attachment: Stored procedure sp_AddMsg code
CREATE PROCEDURE dbo.sp_AddMsg --Import information of foreign companies
(
@in_CompanyName Varchar(100), --Company Name
@in_Address Varchar(200), --Company Address
@in_City Varchar(100), --City of the company
…
)
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 has been explained. I hope this article can bring some help to readers.
1. Introduction to FSO
The FSO object model contains the following types of objects:
Drive object: allows the collection of available space, shared names and other information of hard disks, CD-ROMs and other drives physically or through the system logic connected to the system through the system.
Folder object: allows creating, deleting or moving folders, and querying the system for the name, path, etc. of the folder.
Files object: allows the creation, deletion or movement of files, and query the system for the name, path, etc. of the file.
TextStream object: Allows the creation and reading and writing of text files.
FileSystemObject Object: Provides a complete set of methods for drive, folder and file operations. It can be regarded as a collection of the above objects and is often used with them. Many methods associated with this object repeat the methods in the previous four objects, so we can perform most operations on drives, folders, and files through the FileSystemObject object, or operate on these components through the corresponding drives, folders, or file objects. The FSO model implements operations on the same object through two methods, and its operational 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 model.
(I) 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("")
2. Use FileSystemObject to get text file object TextStream
FileSystemObject provides two methods for getting text file objects TextStream, which is used to create
The file is CreateTextFile, and the file is OpenTextFile. The result of both methods is
An instance of a TextStream object, using this object to perform specific files operations.
⑴ Create a new file
The specific format of the method to create a new file is (taking AFileSystemObject as an example):
(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.
Add file name, such as C:\webshare\aspsamp\
OverwriteExistingFile is a Boolean value indicating whether to overwrite if a file with the same name exists.
Original file. This parameter can be omitted, and by default it is False, that is, 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 by default it is False, that is, an ASCII file.
⑵ Open an existing file
The specific format of the method to open an existing file is (taking AFileSystemObject as an example):
(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.
Add file name, C:\filepath\
IOMode is a constant value, indicating the purpose of opening the file, and ForReading(1) indicates it is used to read data;
ForAppending means to increase data. This parameter can be omitted, and by default it is ForReading.
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 by default it is False, that is, no new file is created.
Format represents the way the file is opened. The possible values and meanings are as follows:
TristateTrue: Open in Unicode mode.
TristateFalse: Open as ASCII.
TristateUseDefault: Open in the default mode of the system.
This parameter can be omitted, and by default it is TristateFalse, that is, ASCII method.
(two). Use TextStream to perform file operations
After creating or opening the file, you can use the methods provided by the object TextStream to perform actual operation of the file.
1. The methods used for writing operations are:
⑴ Write(string)
Write the string specified by string to the file.
⑵ WriteLine(string)
Write a string specified by string in the file and write a newline character.
The parameter string can be omitted, and a blank line will be inserted into the file at this time.
⑶ WriteBlankLines(NumOfLines)
Insert several blank lines into the file, the number of lines is specified by NumOfLines.
2. The methods and attribute methods used for reading operations are:
⑴ AtEndOfLine
This property is a Boolean value indicating whether the file pointer has pointed to the end of 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 indicates the position of the file pointer in the current line.
⑷ Line
This property is an integer value that represents the line number of the line where the file pointer is located.
⑸ Read(NumOfCharacters)
This method starts from the current location of the file, reads in several characters specified by the number of NumOfCharacters, and returns a
String.
⑹ ReadLine
This method starts from the current location of the file, reads the content of the current line until the end of the line, and returns a string.
⑺ ReadAll
This method starts from the current location, 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 location of the file and skips several characters specified by the number of NumOfCharacters.
⑼ SKipLine
This method starts from the current location of the file and skips the content of the current line.
3. The methods used to close files are:
⑴ Close
Close the created or opened file.
(3) Here is an example to illustrate how to use FSO to read text files and save them to the database:
1. Create a page that reads the file path first:
…
<FORM METHOD=POST ACTION="" >
<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 obtained text value to the database code:
<%@Language="VBScript"%>
<%=true%>
<!--#include file=""-->
<%
strConn="DSN=DataSourceName"
set Conn=("")
strConn
set ObjComm=("")
="sp_AddMsg" 'Call stored procedure
=adCmdStoredProc
Set =Conn
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set ObjParamECom=("WC_ECompanyName",adVarchar,adParamInput,100)
ObjParamECom
'@in_ECompanyName Varchar(50), --Company English name
Set ObjParamAddr=("WC_Address",adVarchar,adParamInput,200)
ObjParamAddr
'@in_Address Varchar(50), --Company Address
Set ObjParamCity=("WC_City",adVarchar,adParamInput,100)
ObjParamCity
'@in_City Varchar(50), --City
…
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
%>
<%
dim AllText,strLine1,strLine2,strLine3
dim strpath,fileurl
fileurl=""
strpath=Trim(("path"))
fileurl=strpath
SET FSO=CreateObject("")
SET ATextStream=(fileurl,1,false,TristateFalse)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
DO WHILE NOT
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
strLine1=""
strLine2=""
strLine3=""
…
'''''''''''''''''''''''''''''''
(11)
strLine1=Trim()
(11)
strLine2=Trim()
(5)
strLine3=Trim()
…
'End if
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
=strLine1
=strLine2
=strLine3
…
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
() 'Run command
LOOP
"<br>"+"Import the library successfully! <a href=>[Continue to import]</a><br>"
set Conn=nothing
set FSO=nothing
set ATextStream=nothing
%>
Attachment: Stored procedure sp_AddMsg code
CREATE PROCEDURE dbo.sp_AddMsg --Import information of foreign companies
(
@in_CompanyName Varchar(100), --Company Name
@in_Address Varchar(200), --Company Address
@in_City Varchar(100), --City of the company
…
)
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 has been explained. I hope this article can bring some help to readers.