ASP file operations

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 commonly used objects. To view more detailed information, click here to download the FileSystemObject reference. 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 the file
dim FSO, F
set FSO = server. createobject ("scripting. fileSystemObject ")
set f = FSO. opentextfile ("C: \ test.txt", 1, false) 'Second parameter 1 indicates read-only access, and 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 it
F. skipline () 'moves the current position to the first character of the next line. Note: No parameter
response. write F. read (3) 'read three characters from the current position and move the current position to the next three characters
response. write F. readline () 'reads from the current position backward until it encounters a line break (do not read the line break) and moves the current position to the first character of the next line. Note: No parameter
respo AUC. 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
Createtextfile
(Filename, overwrite, Unicode)
Create a new text file in the folder with the specified file name and return a corresponding textstream object. If the optional overwrite parameter is set to true, it overwrites any existing files with the same name. The default overwrite parameter is false. If the optional Unicode parameter is set to true, the file content is stored as Unicode text. The default Unicode value is false.

You can use the parentfolder attribute of the current folder between folders to return to the parent directory. When a folder arrives, if the isrootfolder attribute is true, it stops. Exit the root directory of the drive and go down the directory tree to traverse or access the specified folder in the folders set (returned by the subfolders attribute of the current folder.
Below Program It traverses all the folders in the drive C root directory and displays information about each folder.
The VBScript program is as follows:
'In VBScript:
'Create a FileSystemObject instance
Set objfso = server. Createobject ("scripting. FileSystemObject ")
'Get a reference to drive C
Set objdrivec = objfso. getdrive ("C :")
'Get a reference to the root folder
Set objroot = objdrivec. rootfolder
'Get a reference to the subfolders collection
Set objfolders = objroot. subfolders
'Get a reference to the first folder in the subfolders collection
For each objfolder in objfolders
Set objfolder1 = objfolders. Item (objfolder. Name ))
Exit
Next
'Iterate through all the files in this folder
For each objfile in objfolder1.files
Response. Write "name:" & objfile. Name &""
Response. Write "shortname:" & objfile. shortname &""
Response. Write "Size:" & objfile. Size & "bytes"
Response. Write "type:" & objfile. Type & "<br>"
Response. Write "Path:" & objfile. Path &""
Response. Write "Export path:" & objfile. Export Path & "<br>"
Response. Write "created:" & objfile. datecreated &""
Response. Write "lastmodified:" & objfile. datelastmodified & "<p>"
Next
The jscript program is as follows:
// In JScript:
// Create a FileSystemObject instance
VaR objfso = server. Createobject ('scripting. filesystemobobject ');
// Get a reference to drive C
VaR objdrivec = objfso. getdrive ('C :');
// Get a reference to the root folder
VaR objroot = objdrivec. rootfolder;
// Get a reference to the first folder in the subfolders collection
VaR colallfolders = new enumerator (objroot. subfolders );
VaR objfolder1 = colallfolders. Item ();
// Get a reference to the files collection for this folder
VaR colfiles = new enumerator (objfolder1.files );

// Iterate through all the files in this collection
For (;! Colfiles. atend (); colfiles. movenext ()){
Objfile = colfiles. Item ()
Response. Write ('name: '+ objfile. Name + '');
Response. Write ('shortname: '+ objfile. shortname + '');
Response. Write ('size: '+ objfile. Size + 'bytes ');
Response. Write ('Type: '+ objfile. Type +' <br> ');
Response. Write ('path: '+ objfile. Path + '');
Response. Write ('export path: '+ objfile. Export path +' <br> ');
Response. Write ('created: '+ objfile. datecreated + '');
Response. Write ('accessed: '+ objfile. datelastaccessed + '');
Response. Write ('Modified: '+ objfile. datelastmodified +' <p> ');
}

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.