FSO Use tutorial 4--How to open a file using the FSO

Source: Internet
Author: User
Tags file copy readline
fso| Tutorial

How to open a file using the FSO -fso Use tutorial 4

After understanding the file copy, delete, move, and rename, let's learn how to access the data in the file. The TextStream object that must be used for file access is a FileSystemObject child object. The method has two kinds of opentextfile and createtextfile, the use example is as follows:

' first set up the FileSystemObject object
Set fs = Server.CreateObject ("Scripting.FileSystemObject")
' Create a TextStream type Object again
Set txt = fs. OpenTextFile(Parameter ...)

Or:

Set fs = Server.CreateObject ("Scripting.FileSystemObject")
Set txt = fs. CreateTextFile(Parameter ...)

After we have established the TextStream object, we can then invoke the ReadLine, WriteLine, Read, ReadAll, Write ... that are attached to the TextStream object. and other methods to access the contents of the file.

Four, the following we first explain if you open the file with the FSO:

Method: OpenTextFile

Call Format:

OpenTextFile (file [, open mode] [, whether the file is created automatically])

Note: (The latter two parameters can be omitted)

    • Open mode: You can have three sets of values:
Setting values Significance
1 Open as read-only file
2 Open into a write-only file, the original file content will be cleared first
8 Opens into a write-only file and retains the original file contents, and the data is written from the last face of the file

If the file is opened with a read-only file, then we can only invoke the ReadLine, read, and ReadAll methods of the TextStream object to read the contents of the file, and if the file opens as a write-only file, You can only invoke the WriteLine and write methods to write data to a file. The omitted state indicates that the file is opened as a read-only file.

    • Whether to create files automatically:

You can set Xuan to true or false, and if set to True, a new file is automatically created and opened when the file is opened, and if set to False, the open file must be an existing file, or a "file cannot be found" error (Error encoded =53) is generated. An omitted state indicates that an open file must be an existing file.

SOURCE Example:

<%
'================================================
' Author: Arisisi
' URL: http://www.alixixi.com/
' Source: FSO move file, rename file example
' Time: December 17, 2005
'================================================
Set fs = Server.CreateObject ("Scripting.FileSystemObject")
File = Server.MapPath ("File1.txt")

' Turn File1.txt into a read-only file, File1.txt must be a file that already exists
Set txt = fs. OpenTextFile (File) ' parameter two or three can be omitted

' Turn File.txt into a read-only file, File1.txt if it does not exist, create the file first
Set txt = fs. OpenTextFile (File,1,true)

' Turn File.txt into a write-only file, File1.txt if it doesn't exist, create the file first
Set txt = fs. OpenTextFile (File,2,true)

' Turn File.txt into a write-only file, File1.txt must be an existing file
Set txt = fs. OpenTextFile (file,8)
%>



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.