Use of asp fso built-in Components

Source: Internet
Author: User

The abbreviation of FSO-FileSystemObject or scripting. FileSystemObject is the built-in IIS component used to operate disks, folders, or text files. FSO has many objects, methods, and attributes. Here we use examples to list common objects. Note: In VBScript language reference or JScript Language Reference: fileSystemObject User Guide and scripting Runtime Library Reference are the complete FileSystemObject reference provided by Microsoft.

FSO cannot operate binary files. To operate binary files, use ADODB. Stream.

Create a file

Dim FSO, F

Set FSO = server. Createobject ("scripting. FileSystemObject ")

Set F = FSO. createtextfile ("C: \ test.txt", true) 'the second parameter indicates whether to overwrite the target file when it exists.

F. Write ("write content ")

F. writeline ("write content and wrap ")

F. writeblanklines (3) 'write three blank rows (equivalent to pressing the carriage return three times in the text editor)

F. Close ()

Set F = nothing

Set FSO = nothing

Open and read files

Dim FSO, F

Set FSO = server. Createobject ("scripting. FileSystemObject ")

Set F = FSO. opentextfile ("C: \ test.txt", 1, false) 'the second parameter 1 indicates read-only access, and the third parameter indicates whether to create a file when the target file does not exist

F. Skip (3) 'moves the current position three characters behind

F. skipline () 'moves the current position to the first character of the next line. Note: No Parameter

Response. Write F. Read (3) 'reads three characters from the current position and moves the current position to the next position.

Response. Write F. Readline () 'reads backward from the current position until a line break is encountered (do not read the line break), and moves the current position to the first character of the next line. Note: No Parameter

Response. Write F. readall () 'reads from the current position backward until the end of the file and moves the current position to the end of the file.

If F. atendofline then

Response. Write ("end of a row! ")

End if

If F. atendofstream then

Response. Write ("The end of the file! ")

End if

F. Close ()

Set F = nothing

Set FSO = nothing

Open and write files

Dim FSO, F

Set FSO = server. Createobject ("scripting. FileSystemObject ")

Set F = FSO. opentextfile ("C: \ test.txt", 2, false) 'the second parameter 2 indicates rewriting. If it is 8, it indicates appending.

F. Write ("write content ")

F. writeline ("write content and wrap ")

F. writeblanklines (3) 'write three blank rows (equivalent to pressing the carriage return three times in the text editor)

F. Close ()

Set F = nothing

Set FSO = nothing

Determine whether a file exists

Dim FSO

Set FSO = server. Createobject ("scripting. FileSystemObject ")

If FSO. fileexists ("C: \ test.txt") then

Response. Write ("target file exists ")

Else

Response. Write ("the target file does not exist ")

End if

Set FSO = nothing

Move files

Dim FSO

Set FSO = server. Createobject ("scripting. FileSystemObject ")

Call FSO. movefile ("C: \ test.txt", "d: \ test111.txt") 'two parameters have different file names.

Set FSO = nothing

Copy a file

Dim FSO

Set FSO = server. Createobject ("scripting. FileSystemObject ")

Call FSO. copyfile ("C: \ test.txt", "d: \ test111.txt") 'two parameters have different file names.

Set FSO = nothing

Delete an object

Dim FSO

Set FSO = server. Createobject ("scripting. FileSystemObject ")

FSO. deletefile ("C: \ test.txt ")

Set FSO = nothing

Create a folder

Dim FSO

Set FSO = server. Createobject ("scripting. FileSystemObject ")

FSO. createfolder ("C: \ test") 'the parent folder of the target folder must exist.

Set FSO = nothing

Determine whether a folder exists

Dim FSO

Set FSO = server. Createobject ("scripting. FileSystemObject ")

If FSO. folderexists ("C: \ Windows") then

Response. Write ("target folder exists ")

Else

Response. Write ("the target Folder does not exist ")

End if

Set FSO = nothing

Delete folder

Dim FSO

Set FSO = server. Createobject ("scripting. FileSystemObject ")

FSO. deletefolder ("C: \ test") 'folder does not need to be empty

Set FSO = nothing

Related Article

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.