ASP Getting Started tutorials-file actions

Source: Internet
Author: User

File operations mainly include copying, moving, and deleting files, checking for the existence of files, and obtaining properties of files. You can do this by using the FileSystemObject or the method of the File object.

First, use the FileSystemObject object's corresponding method to do the file operation

1, copy the file: Copy one or more files to another location, the syntax format is as follows:

<% fso.CopyFile source,destination[,overwrite] %>

* FSO: Specifies the name of the FileSystemObject object;

* Source: A string representing the specified file in which a wildcard character can be used to copy one or more files;

* Destination: A string representing the destination, copying the file from source to that location, and not allowing wildcard characters to be used;

* Overwrite: is an optional parameter, Boolean value indicating whether to overwrite the existing file: if it is True (the default), overwrite the file, and if False, do not overwrite the existing file.

Note: If Destinatiion is set as a read-only property, the CopyFile operation cannot be completed regardless of the value of the overwrite parameter setting.

Wildcard characters can only be used in the last part of the path of the Source parameter.

2, moving files: One or more files from one location to another location, the syntax format is as follows:

<% fso.MoveFile source,destination %>

* FSO: Specifies the name of the FileSystemObject object;

* Source: Specifies the path of the file to be moved, which can only be used with wildcard characters in the last part of the path;

* Destination: Specifies a path that indicates that you want to move the file to that target location, which cannot contain a wildcard character.

Note:

* If the destination does not exist, the file is moved;

* If destination is a file that already exists, an error occurs;

* If the destination is a directory, an error occurs;

* If source uses wildcard characters but no matching files, an error occurs;

* The MoveFile method stops when it encounters the first error that occurs. This method does not undo any changes that were made before the error occurred.

3, delete files: Delete The specified file, the syntax format is as follows:

fso.DeleteFile filespec[.force]

* FSO: Specifies the name of the FileSystemObject object;

* filespec Specifies the name of the file to be deleted, and the wildcard character can be included in the last part of the path;

* Force: is an optional parameter, Boolean value, True if you want to delete a read-only file, or False (default).;

* If no matching file is found, an error occurs. The DeleteFile method stops when it encounters the first error that occurs. This method does not undo any changes that were made before the error occurred.

4, Instance 1 (5/file.asp) creates a text file and writes the content, and then performs the copy, move, and delete operations.

<% @ language= "VBScript"%>
<body>
<p> is creating ......</p>
<%
Dim Fso,txtfile
Set fso=server.createobject ("Scripting.FileSystemObject")
Set Txtfile=fso. CreateTextFile ("F:mywebflasher123aspframe55file emp1.txt")
Txtfile.writeline ("hello!")
Txtfile.close
%>
<p> file was created successfully and written! </p>
<p> Copying Files .......</p>
<% ' FSO. CopyFile "F:mywebflasher123aspframe55file emp1.txt", "F:mywebflasher123aspframe55file emp2.txt"%>
<% FSO. CopyFile Server.MapPath ("Temp1.txt"), Server.MapPath ("Temp2.txt")%>
<p> Moving Files ......</p>
<% FSO. MoveFile Server.MapPath ("Temp2.txt"), Server.MapPath ("Temp3.txt")%>
<p> Deleting Files ......</p>
<%
Fso.deletefile Server.MapPath ("Temp1.txt")
Fso. DeleteFile Server.MapPath ("Temp3.txt")
%>
<p> Delete operation completed! </p>
</body>

Manipulating files using the method of the File object

1, the method of the File object

Method of the File object

Method Description/Expression/annotation
Cope Copies the specified file or folder from one location to another.
Object. Copy Destination[,overwrite]
Parameter object specifies the name of the file or folder object, destination specifies the destination of the copied files or folders, the wildcard character is not allowed, overwrite is an optional parameter, a Boolean value, to overwrite an existing file or file, set the parameter to True (default), otherwise set to False.
Move Moves the specified file or folder from one location to another.
Object. Move destination
Parameter object specifies the name of the file or folder object, destination specifies the destination of the copied files or folders, and does not allow the use of a wildcard.
Delete Deletes the specified file or folder.
Object. Delete Force
The Parameter object specifies the name of the file or Folder object. Force is an optional argument, Boolean value, True if the property of the file or folder you want to delete is set to read-only, or False (default).

Note: Before using the method of a file object to manipulate a file, you should return the file object corresponding to the specified path with the GetFile method of the FileSystemObject object. The syntax format is as follows:

object.GetFile(filespec)

object is the name of the FileSystemObject object. FILESPEC Specifies the path (absolute or relative) of the file.

2, Instance 2 (4/file.asp): This example shows how to use the method of the file object to perform copy, move, and delete operations on files.

<%
dim fso,txtFile,afile
Set fso=Server.CreateObject("Scripting.FileSystemObject")
'创建一个文件,并复制、移动和删除使用
Set txtFile=fso.CreateTextFile(Server.MapPath("temt1.txt"))
txtFile.WriteLine("Hello!")
txtFile.Close
Set afile=fso.GetFile(Server.MapPath("temt1.txt"))
'复制文件
afile.Copy (Server.MapPath("temt2.txt")),true
'移动文件
afile.Move (Server.MapPath("temt3.txt"))
'删除文件
afile.Delete
%>
<body>

1. Create File Temt1.txt;<br>

2, open temt1.txt file;<br>

3, create the file Temt1.txt instance object. <br>

4, copy the Temt1.txt file, copy the file name is temt2.txt;<br>

5, the file Temt1.txt moved to file temt3.txt. File TEMT1 is replaced by file text3.txt. <br>

6, delete the file temt3.txt. Because we created the file Temt1.txt object with the GetFile method, the Temt1.txt is replaced by the file temt3.txt. So the deletion is the file temt3.txt. <br>

</body>

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.