Tutorials to .com

Tutorials to .com » Asp » Base » Advanced Programming ASP 3.0 (21)

Advanced Programming ASP 3.0 (21)

Print View , by: iSee ,Total views: 10 ,Word Count: 3042 ,Date: Sat, 25 Apr 2009 Time: 11:48 PM

1. Folder object
Driver attributes RootFolder object returns a Folder object through the object can access the drive contents of all. Can use the Folder object's properties and methods of traversing a directory on the drive, and the folder and other attributes of the folder.
(1) Folder object properties
Folder object provides a set of attributes, these attributes can be used on the current folder For more information, you can change the name of the folder. And describe its properties as shown in Table 5-9:
Table 5-9 Folder object's properties and description
Properties
Description

Attributes
Return to the properties of the folder. Value can be one of the following or their combination: Normal (0), ReadOnly (1), Hidden (2), system (4), Volume (name) (8), Directory (folder) (16), Archive ( 32), Alias (64) and Compressed (128). For example, a hidden read-only document, Attributes of a value of 3

DateCreated
Return to the folder creation date and time

DateLastAccessed
Return visit to the last folder date and time

DateLastModified
Return to the last time the folder modification date and time

Drive
Return to the folder where the drive letter drive

Files
Return to Folder object contains a collection of Files, the folder that all the documents

IsRootFolder
Returns a boolean value of the folder is the root drive folder

Name
Set or return the name of the folder

ParentFolder
Return to the folder corresponding to the parent folder of the Folder object

Path
Return the absolute path of the folder, use the long file name

ShortName
Return to DOS-style form of 8.3 folder name

ShortPath
Return to the form of DOS-style 8.3 folder absolute path

Size
Return to the folder that contains all the files and the size of subfolders

SubFolers
Return to the folder contains subfolders corresponding to all of the Folders collection, including the hidden folders and system folders

Type
If possible, returns a string description of the folder (for example, "Recycle Bin")

(2) Folder object
Folder object can be used to provide a copy, delete and move methods of the current folder. The operation of these methods with the FileSystemObject object CopyFolder, DeleFolder and MoveFolder the same, but these methods do not require source parameters, as is the source file folder. And description of these methods, such as shown in Table 5-10:
Table 5-10 Folder object and description
Methods
Description

Copy (destination, overwrite)
To this folder and copy all the contents of the specified destination folder. If the destination is at the end of the path separator ( '\'), then that is the destination folder to place copies of a folder. Otherwise, that destination is to create a new path and folder name. If the target folder already exists and overwrite parameter is set to False, will result in error, overwrite the default parameter is True

Delete (force)
Folder and delete all the contents inside. Force if the optional parameter is set to True, even if the folder is set to read-only or read-only documents contain will also delete the folder. Force the default is False

Move (destination)
Will be inside the folder and move all the contents of the specified destination folder. If the destination is at the end of the path separator ( '\'), then that is the destination folder to place a mobile folder. Otherwise, that destination is a new path and folder name. If the target folder already exists, then an error

CreateTextFile
(filename, overwrite, unicode)
The file name specified in the folder to create a new text file and return a corresponding object TextStream. If the optional overwrite parameter is set to True, will cover any existing file with the same name. Overwrite the default parameter is False. If unicode optional parameters set to True, the contents of the documents will be stored as unicode text. Unicode default is False

In between the folder can use the current folder properties of ParentFolder to return to the parent directory. When the arrival of a folder, if IsRootFolder attribute is True, stopped. To leave the root directory of the drive along the tree down, or access to traverse in the Folders collection (from the current folder to return to the SubFolders attributes) within the specified folder.
The following procedures traverse the root directory of drive c with all folders, each folder and display the relevant information.
vbscript procedures are 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 For
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 "ShortPath:" & objFile.ShortPath & "<BR>"
Response.Write "Created:" & objFile.DateCreated & ""
Response.Write "LastModified:" & objFile.DateLastModified & "<P>"
Next
jscript procedures are as follows:
/ / In JScript:
/ / Create a FileSystemObject instance
var objFSO = Server.CreateObject ( 'Scripting.FileSystemObject');
/ / 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 ( 'ShortPath:' + objFile.ShortPath + '<BR>');
Response.Write ( 'Created:' + objFile.DateCreated + '');
Response.Write ( 'Accessed:' + objFile.DateLastAccessed + '');
Response.Write ( 'Modified:' + objFile.DateLastModified + '<P>');
)
The VBScript program to run on the server when the results shown in Figure 5-12. The page is folderscollection_vb.asp, from the book provided sample file.

Figure 5-12 Folders collection of the contents of
(3) the use of special folders
FileSystemObject Object GetSpecialFolder is one of the ways, it returned to the computer in three "special folder" Folder object corresponding to:
WindowsFolder:% Windows% directory, the default for WinNT (or Windows, the computer in a non-NT/2000) directory.
SystemFolder:% System% directory, the default for the WinNT \ System32 (or Windows \ System, a computer in a non-NT/2000) directory.
TemporaryFolder:% Temp% directory, the default for the WinNT \ Temp (or Windows \ Temp, the computer in a non-NT/2000) directory.
To get special folder of references, we provide pre-defined constants of the corresponding methods as GetSpecialFolder parameters:
'In VBScript:
Set objFSO = Server.CreateObject ( "Scripting.FileSystemObject")

Set objFolder = objFSO.GetSpecialFolder (WindowsFolder)
Response.Write "GetSpecialFolder (WindowsFolder) returned: <BR>"
Response.Write "Path:" & objFolder.Path & "<BR>"
Response.Write "Type:" & objFolder.Type & "<P>"

Set objFolder = objFSO.GetSpecialFolder (SystemFolder)
Response.Write "GetSpecialFolder (SystemFolder) returned: <BR>"
Response.Write "Path:" & objFolder.Path & "<BR>"
Response.Write "Type:" & objFolder.Type & "<P>"

Set objFolder = objFSO.GetSpecialFolder (TemporaryFolder)
Response.Write "GetSpecialFolder (TemporaryFolder) returned: <BR>"
Response.Write "Path:" & objFolder.Path & "<BR>"
Response.Write "Type:" & objFolder.Type & "<P>"
Or JScript:
/ / In JScript:
var objFSO = Server.CreateObject ( 'Scripting.FileSystemObject');

var objFolder = objFSO.GetSpecialFolder (WindowsFolder);
Response.Write ( 'GetSpecialFolder (WindowsFolder) returned -');
Response.Write ( 'Path:' + objFolder.Path + '');
Response.Write ( 'Type:' + objFolder.Type + '<BR>');

var objFolder = objFSO.GetSpecialFolder (SystemFolder);
Response.Write ( 'GetSpecialFolder (SystemFolder) returned -');
Response.Write ( 'Path:' + objFolder.Path + '');
Response.Write ( 'Type:' + objFolder.Type + '<BR>');

var objFolder = objFSO.GetSpecialFolder (TemporaryFolder);
Response.Write ( 'GetSpecialFolder (TemporaryFolder) returned -');
Response.Write ( 'Path:' + objFolder.Path + '');
Response.Write ( 'Type:' + objFolder.Type + '<BR>');
The VBScript program to run on the server when the results shown in Figure 5-13. The page called specialfolder_vb.asp, from the book provided sample file.

Figure 5-13 GetSpecialFolder the results of methods
2. File Object
File object provides access to the properties of paper, through its method of operation of the document. Each Folder object provides a Files collection contains documents folder corresponding File object. Can also directly from the FileSystemObject Object GetFile method by using a File object reference.
(1) File object's properties
File objects have a series of attributes, similar to Folder object's properties, such as shown in Table 5-11:
Table 5-11 File object's properties and description
Properties
Description

Attributes
Return document attributes. Value can be one of the following or their combination: Normal (0), ReadOnly (1), Hidden (2), System (4), Volume (name) (9), Directory (folder) (16), Archive ( 32), Alias (64) and Compressed (128)

DateCreated
Return to the folder creation date and time

DateLastAccessed
Return to the last visit of the document the date and time

DateLastModified
Return to the last modification of the document the date and time

Drive
Return the document in which the target drive Drive

Name
Set or return the name

ParentFolder
Return to the parent folder of documents of the Folder object

Path
Return the absolute file path, you can use long file names

ShortName
Return to the form of DOS-style 8.3 file names

ShortPath
8.3 Return to DOS-style absolute path form of a document

Size
Return to the file size (bytes)

Type
If possible, returns a string description of file types (for example: "Text Document" said. Txt files)

(2) File Object
Folder-like object the same, File object method allows copying, deleting and moving files. It also has a flow to use to open text documents. File object and note as shown in Table 5-12:
Table 5-12 File object's methods and description
Methods
Description

Copy (destination, overwrite)
The document will be copied to the specified destination folder. If the destination is at the end of the path separator ( '\'), so that the destination is to place copies of the document folder. Otherwise, that destination is to create a new file path and name. If the target file already exists and overwrite parameter is set to False, will result in error, overwrite the default parameter is True

Delete (force)
Delete the document. Force if the optional parameter is set to True, even if the document has read-only attribute will be deleted. Force the default is False

Move (destination)
Documents moved to the specified destination folder. If the destination is at the end of the path separator ( '\'), so that the destination is a folder. Otherwise, that is a new destination path and file name. If the target folder already exists, then an error

CreateTextFile
(filename, overwrite, unicode)
With the specified file name to create a new text file and return a corresponding object TextStream. If the optional overwrite parameter is set to True, will cover any existing file with the same name. Overwrite the default parameter is False. If unicode optional parameters set to True, the contents of the documents will be stored as unicode text. Unicode default is False

OpenAsTextStream
(iomode, format)
Open the specified file and returns an object TextStream for file read, write, or additional. iomode parameter specifies the type of access required to allow the value is ForReading (1) (default value), ForWrite (2), ForAppending (8). describes the format parameters to read, write file data format. Allowed value is TristateFalse (0) (default), indicating that data using ASCII format; TristateTrue (-1) that use Unicode data format; TristateUseDefault (-2) use the system default format

Therefore given a File object, you can use ParentFolder attributes to be included in the document Folder object reference to navigation in the file system. Drive can even be used to obtain the corresponding attribute object reference Drive and Folder get a variety of objects, as well as the File object contains.
In addition, to set a Folder object, as well as the corresponding set of Files, you can traverse the collection of checks through the folder of each file. File objects can also use a variety of ways to deal with some of the documents, such as copy, move or delete. The following C code gives the first drive of the file folder list:
'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 For
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 "ShortPath:" & objFile.ShortPath & "<BR>"
Response.Write "Created:" & objFile.DateCreated & ""
Response.Write "LastModified:" & objFile.DateLastModified & "<P>"
Next
Note that the index can not be used to locate the number of Files or Folders entry in the collection, it must use the For Each ... Next statement traversal of the collection until the first entry, and then the purpose of using the Name attribute. Have to use nested parentheses to force as a value (string) to send to the Folders collection's Item method.
JScript with the following procedures to be completed by the same work:
/ / In JScript:
/ / Create a FileSystemObject instance
var objFSO = Server.CreateObject ( 'Scripting.FileSystemObject');
/ / 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 ( 'ShortPath:' + objFile.ShortPath + '<BR>');
Response.Write ( 'Created:' + objFile.DateCreated + '');
Response.Write ( 'Accessed:' + objFile.DateLastAccessed + '');
Response.Write ( 'Modified:' + objFile.DateLastModified + '<P>');
)
The results of the two procedures are the same, as shown in Figure 5-14. The page is filescollection_vb.asp, from the book provided sample file.

Figure 5-14 File content collection

5.5 Scripting.TextStream object
FileSystemObject, Folder and File objects and the adoption of a number of methods TextStream object to create, read or write documents.
Although TextStream object FileSystemObject object is defined as an independent subsidiary, but we have to use the FileSystemObject object or its subsidiaries TextStream object to create a document object and access the contents of the disk.


Asp Base Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.