Summary of FileSystemObject object members

Source: Internet
Author: User
Summary of FileSystemObject object members
The FileSystemObject object provides an attribute and a series of methods to manipulate some of the dependent objects implemented by the FileSystemObject object. Here we provide a summary of all the content, and then introduce each subordinate object.
1. Attributes of FileSystemObject
The FileSystemObject object has only one attribute, which is used to obtain a list of all valid drives on the current machine, as shown in table 5-4:
Table 5-4 attributes and descriptions of FileSystemObject
Genus
Description

Drivers
Returns the list of available drives on the local computer.

2. FileSystemObject Method
The FileSystemObject object provides a series of methods for using a subordinate object, including objects such as drive, folder, and file. It also implements two methods for textstream objects: createtextfile and opentextfile. Methods are divided into three types based on the type of objects used.
(1) methods related to the drive
The methods related to the drive are shown in Table 5-5:
Table 5-5 drive-related methods and instructions
Method
Description

Driveexists (drivespec)
If the drive specified in drivespec exists, true is returned; otherwise, false is returned. The drivespec parameter can be a drive letter or a complete absolute path to a file or folder.

Getdrive (drivespec)
Returns the drive object corresponding to the drive specified by drivespec. Drivespec can contain colons, path delimiters, or network sharing names, such as "C", "C:", "C:", and "\ machinesharename"

Getdrivename (drivespec)
Returns the name of the drive specified by drivespec with a string. The drivespec parameter must be an absolute path to a file or folder, or only a drive letter, for example, "C:" or "C"

(2) folder-related methods
The folder-related methods are shown in Table 5-6:
Table 5-6 folder-related methods and instructions
Method
Description

Buildpath (path, name)
Add a file or folder named name to the existing Path. If necessary, add the path separator''

Copyfolder (source, destination, overwrite)
Copy one or more folders from the specified source folder source (which can contain wildcards) to the specified destination folder destination, which contains all files in the source folder. If the source contains a wildcard or the path separator ('') at the end of destination, destination is considered to be the folder to be copied from the source folder. Otherwise, destination is the path name of the new folder to be created. If the destination folder already exists and the overwrite parameter is set to false, an error is generated. The default overwrite parameter is true.

Createfolder (Foldername)
Create a folder named Foldername. An error occurs if the Foldername already exists.

Deletefolder (folderspec, force)
Delete one or more folders specified by folderspec (the last part of the path can contain a pattern) and all contents in the folder. If the optional force parameter is set to true, the folder will be deleted even if the file contained in the folder has a read-only attribute. The default force parameter is false.

Folderexist (folderspec)
If the folder specified by folderspec exists, true is returned; otherwise, false is returned. The folderspec parameter can contain the absolute or relative path of the folder, or only the folder name seen in the current folder.

Getabsolutepathname (pathspec)
Specify the path of the folder, which must take into account the path of the current folder. For example, if the current folder is "C: docssales" and the pathspec is "Jan", the returned character is "C: docssalesjan ". Wildcards, "...", and "\" path operators are acceptable.

Getfolder (folderspec)
Returns the folder object corresponding to the folder specified by folderspec. Folderspec can be the relative or absolute path of the folder.

Getparentfoldername (pathspec)
Returns the upper-level folder of the pathspec file or folder. Check whether the folder exists

Getspecialfolder (folderspec)
Returns the folder object corresponding to a specific Windows folder. The allowed values of the folderspec parameter are windowsfolder (0), systemfolder (1), and temporaryfolder (2)

Movefolder (source, destination)
Move one or more folders specified by source to the folder specified by destination. Wildcards can be contained in source, but not in destination. If source contains wildcards or destination ends with a path separator (''), destination is considered to be the folder where the source folder is to be placed; otherwise, destination is considered to be the complete path and name of a new folder. If the destination folder destination already exists, an error is returned.

(3) file-related methods
The file-related methods are shown in Table 5-7:
Method
Description

Copyfile (source, destination,
Overwrite)
Copy one or more files specified by source (which can contain wildcards) to the destination folder destination. If the source contains a wildcard or the path separator ('') at the end of destination, destination is considered a folder. Otherwise, destination is the full path and name of the new file. If the target folder already exists and the overwrite parameter is set to false, an error is returned. The default overwrite parameter is true.

Createtextfile (filename, overwrite,
Unicode)
Create a new text file on the disk with the specified filename and return the corresponding textstream object. If the optional overwrite parameter is set to true, the existing files with the same name in the same path are overwritten. The default overwrite parameter is false. If the optional Unicode parameter is set to true, the content of the file is stored as Unicode text. The default Unicode parameter is false.

Delefile (filespec, force)
Delete one or more files specified by filespec (wildcards can be included at the end of the path ). If the optional force parameter is set to true, files with read-only attributes are also deleted. The default force parameter is false.

Fileexists (filespec)
If a file specified by filespec exists, true is returned; otherwise, false is returned. The filespec parameter can contain the absolute or relative path of the file, or the file name in the current folder.

Getbasename (filespec)
Returns the name of the file specified by filespec, that is, the file path is included, but the file extension is removed.

Getextensionname (filespec)
Returns the file extension specified by filespec.

GetFile (filespec)
Returns the file object corresponding to the file specified by filespec. You can specify the relative or absolute path of an object.

Getfilename (pathspec)
Returns the path or file name of the file specified by patheatec. If there is no file name, the last folder name is returned. Do not check whether the file or folder exists

Gettempname ()
Returns a random file name used to complete the operation.

Movefile (source, destination)
Move one or more source files specified by source to the destination folder specified by destination. Wildcards can be contained in source, but destination does not. If the source contains a path separator ('') at the end of the provisioning or destination, destination is considered as a folder. Otherwise, destination is the complete path and name of the new folder. An error occurs if the destination folder already exists.

Opentextfile (filename, iomode, create,
Format)
Create a file named filename, or open an existing file named filename, and return a textstream object related to it. The filename parameter can contain absolute or relative paths. The iomode parameter specifies the required access type. The allowed values are forreading (1) (default), forwriting (2), and forappending (8 ). When writing or appending data to a non-existent file, if the create parameter is set to true, a new file is created. The default create parameter is false. The format parameter specifies the format of data read or written to a file. Allowed value: tristatetfalse (0) (default), which is enabled in ASCII format; tristatettrue (-1), which is enabled in unicode format; tristatedefault (-2), which is enabled in the system default format

Unicode files use two bytes to identify each character, removing the limit of up to 256 ASCII characters.

5.4.2 use drive
The following is a simple example of using the FileSystemObject object. It uses the driveexists method to obtain a list of existing drive letters:
'In VBScript
Set objfso = server. Createobject ("scripting. FileSystemObject ")
For intcode = 65 to 90 'ansi codes for 'a' to 'Z'
Strletter = CHR (intcode)
If objfso. driveexists (strletter) then
Response. Write "found drive" & strletter & ": <br>"
End if
Next
Or use JScript:
// In JScript
VaR objfso = server. Createobject ('scripting. filesystemobobject ');
For (VAR intcode = 65; intcode <= 90; intcode ++) {// ANSI codes for 'a' to 'Z'
Strletter = string. formcharcode (intcode );
If (objfso. driveexists (strletter ))
Response. Write ('found drive '+ strletter + ": <br> ");
}
The two Program The running results of the segments are the same, as shown in Figure 5-9:

Figure 5-9 drive list
This page is driveexists_vb.asp, which is provided by the example file in this book.
1. Drive object
As you can see, the FileSystemObject object contains an attribute ?? Drives, which returns a collection of all available drives on the local computer.
Each entry in the drives set is a drive object. The attributes of the drive object are shown in Table 5-8:
Table 5-8 attributes and descriptions of drive objects
Genus
Description

Availablespave
Returns the size of the space available to the user on the drive considering the account quota and/or other restrictions

Driveletter
Returns the letter of the drive.

Drivetype
Returns the type of the drive. The returned values can be unknown (0), removeable (1), fixed (2), Network (3), CDROM (4), and ramdisk (5 ). However, the current version of scrrun. dll does not support the predefined constant network, which must be replaced by decimal 3.

Filesystem
Returns the type of the drive file system. The returned values include "fat", "NTFS", and "CDFs"

Freespace
Returns the total available space on the drive.

Isready
Returns a Boolean value indicating whether the drive is ready

Path
Returns a drive path consisting of letters and colons, namely, "C :"

Rootfolder
Returns the folder object in the root folder of the drive.

Serialnumber
Returns a decimal serial number used to identify the disk volume.

Sharename
If it is a network drive, the Network Share Name of the drive is returned

Totalsize
Total size of the returned drive (in bytes)

Volumename
Set or return the name of the local drive volume

Therefore, by using the drive object in the drives set, you can generate a drive list on the server, which is more efficient than determining whether the drive exists by checking every possible drive letter. We can also get information about the drive. In VBScript,CodeAs follows:
'In VBScript:
'Create a FileSystemObject instance
Set objfso = server. Createobject ("scripting. FileSystemObject ")
'Create a drives collection
Set coldrives = objfso. Drives
'Iterate through the drives collection
For each objdrive in coldrives

Response. Write "driveletter: <B>" & objdrive. driveletter & "</B> & nbsp ;"
Response. Write "drivetype: <B>" & objdrive. drivetype
Select case objdrive. drivetype
Case 0: Response. Write "-(unknown )"
Case 1: Response. Write "-(removable )"
Case 2: Response. Write "-(fixed )"
Case 3: Response. Write "-(network )"
Case 4: Response. Write "-(CDROM )"
Case 5: Response. Write "-(ramdisk )"
End select
Response. Write "</B> & nbsp ;"

If objdrive. drivetype = 3 then
If objdrive. isready then
Response. Write "Remote Drive with sharename: <B>" & objdrive. sharename & "</B>"
Else
Response. Write "Remote Drive-<B> isready </B> property returned _
<B> false </B> <br>"
End if
Else if objdrive. isready then
Response. Write "filesystem: <B>" & objdrive. filesystem & "</B> & nbsp ;"
Response. Write "serialnumber: <B>" & objdrive. serialnumber & "</B> <br>"
Response. Write "local drive with volumename: <B> "&_
Objdrive. volumename & "</B> <br>"
Response. Write "availablespace: <B>" & formatnumber (_
Objdrive. availablespace/1024, 0) & "</B> kb & nbsp ;"
Response. Write "freespace: <B>" & formatnumber (_
Objdrive. freespace/1024, 0) & "</B> kb & nbsp ;"
Response. Write "totalsize: <B>" & formatnumber (_
Objdrive. totalsize/1024, 0) & "</B> kb"
End if
Response. Write "<p>"
End if
Next
Note that the drivetype attribute of the drive cannot be compared with the predefined constant network, because (at least in scrrun. the Network constant is omitted in the Type Library, so it is no longer used as a common constant.
In JScript, the program is:
// In JScript:
// Create a FileSystemObject instance
VaR objfso = server. Createobject ('scripting. filesystemobobject ');
// Create a drives collection
VaR coldrives = new enumerator (objfso. Drives );

For (;! Coldrives. atend (); coldrives. movenext ()){
Objdrive = coldrives. Item ();
Response. Write ('driveletter: '+ objdrive. driveletter +' <br> ');
Response. Write ('drivetype: '+ objdrive. drivetype +' <br> ');

If (objdrive. drivetype = 3)
If (objdrive. isready)
Response. Write ('remote drive with sharename: '+
Objdrive. sharename + '<br> ')
Else
Response. Write ('remote drive-isready property returned false <br> ');
Else if (objdrive. isready ){
Response. Write ('local drive with volumename: '+
Objdrive. volumename + '<br> ');
Response. Write ('filesystem: '+ objdrive. filesystem +' <br> ');
Response. Write ('serialnumber: '+ objdrive. serialnumber +' <br> ');
Response. Write ('availablespace: '+ objdrive. availablespace + 'bytes <br> ');
Response. Write ('freespace: '+ objdrive. freespace + 'bytes <br> ');
Response. Write ('totalsize: '+ objdrive. totalsize + 'bytes <p> ');
}
}
Pay attention to this before running this program on the system. If there is no disk in drive a, or there is no disc in the CD-ROM drive, an error message is displayed: "Disk not ready ". In addition to the driveletter and drivetype attributes, You can protect the page by checking the isready attribute of each drive before using other attributes and methods.
When the above VBScript code is run on the server, the running result is 5-10. This page is drivescollection_vb.asp, from the example file provided in this book.

Figure 5-10 detailed driver list
2. File System Positioning
Several FileSystemObject methods can be used to get references to other objects, so they can be located in the file system of the server and any network drive. In fact, all objects or components used in ASP code, except ActiveX Data Object components, FileSystemObject objects may be one of the most complex objects.
This complexity is due to the high flexibility required to access different parts of the file system. For example, you can locate a file from FileSystemObject down by using various slave objects. The process starts from the drives set, to a drive object, to the root folder object of the drive, and then to the Child folder object, and then to the files set of the folder, finally to the file object in the set.
In addition, if you know the drive, folder, or file to be accessed. You can directly use the getdrive, getfolder, getspecialfolder, and GetFile methods. Figure 5-11 helps you understand the relationship between components, objects, methods, and attributes of all file system positioning.

Figure 5-11 file system positioning

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.