·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP网站建设 >> ASP 3.0高级编程(二十一)

ASP 3.0高级编程(二十一)

作者:佚名      ASP网站建设编辑:admin      更新时间:2022-07-23
1.  Folder对象
Driver对象的RootFolder属性返回一个Folder对象,通过该对象可访问这个驱动器内的所有的内容。可以使用这个Folder对象的属性和方法遍历驱动器上的目录,并得到该文件夹和其他文件夹的属性。
(1)    Folder对象的属性
Folder对象提供一组属性,可用这些属性得到关于当前文件夹的更多信息,也可以改变该文件夹的名称。其属性及说明如表5-9所示:
表5-9  Folder 对象的属性及说明
属 性
说 明

Attributes
返回文件夹的属性。可以是下列值中的一个或其组合:Normal(0)、ReadOnly(1)、Hidden(2)、System(4)、Volume(名称)(8)、Directory(文件夹)(16)、Archive(32)、Alias(64)和ComPRessed(128)。例如,一个隐藏的只读文件,Attributes的值为3

DateCreated
返回该文件夹的创建日期和时间

DateLastaccessed
返回最后一次访问该文件夹的日期和时间

DateLastModified
返回最后一次修改该文件夹的日期和时间

Drive
返回该文件夹所在的驱动器的驱动器字母

Files
返回Folder对象包含的Files集合,表示该文件夹内所有的文件

IsRootFolder
返回一个布尔值说明该文件夹是否是当前驱动器的根文件夹

Name
设定或返回文件夹的名字

ParentFolder
返回该文件夹的父文件夹对应的Folder对象

Path
返回文件夹的绝对路径,使用相应的长文件名

ShortName
返回DOS风格的8.3形式的文件夹名

ShortPath
返回DOS风格的8.3形式的文件夹的绝对路径

Size
返回包含在该文件夹里所有文件和子文件夹的大小

SubFolers
返回该文件夹内包含的所有子文件夹对应的Folders集合,包括隐藏文件夹和系统文件夹

Type
如果可能,返回一个文件夹的说明字符串(例如,“Recycle Bin”)

(2)    Folder对象的方法
Folder对象提供一组可用于复制、删除和移动当前文件夹的方法。这些方法的运行方式与FileSystemObject对象的CopyFolder、DeleFolder和MoveFolder方法相同,但这些方法不要求source参数,因为源文件就是这个文件夹。这些方法及说明如表5-10所示:
表5-10  Folder对象的方法及说明
方 法
说 明

Copy(destination,overwrite)
将这个文件夹及所有的内容复制到destination指定的文件夹。如果destination的末尾是路径分隔符(‘\’),那么认为destination是放置拷贝文件夹的一个文件夹。否则认为destination是要创建的新文件夹的路径和名字。如果目标文件夹已经存在且overwrite参数设置为False,将产生错误,缺省的overwrite参数是True

Delete(force)
删除文件夹及里面的所有内容。如果可选的force参数设置为True,即使文件夹设置为只读或含有只读的文件,也将删除该文件夹。缺省的force是False

Move(destination)
将文件夹及里面所有的内容移动到destination指定的文件夹。如果destination的末尾是路径分隔符(‘\’),那么认为destination是放置移动文件夹的一个文件夹。否则认为destination是一个新的文件夹的路径和名字。如果目标文件夹已经存在,则出错

CreateTextFile
(filename,overwrite,unicode)
用指定的文件名在文件夹内创建一个新的文本文件,并且返回一个相应的TextStream对象。如果可选的overwrite参数设置为True,将覆盖任何已有的同名文件。缺省的overwrite参数是False。如果可选的unicode参数设置为True,文件的内容将存储为unicode文本。缺省的unicode是False

       在文件夹之间可以使用当前文件夹的ParentFolder属性,返回到父目录。当到达一个文件夹时,如果IsRootFolder属性是True,就停下来。离开驱动器的根目录,沿目录树向下,可遍历或访问在Folders集合(由当前文件夹的SubFolders属性返回)内的指定文件夹。
       下列程序遍历了驱动器C根目录内的所有文件夹,并显示各个文件夹的有关信息。
       VBScript程序如下:
       '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 & "&nbsp; &nbsp;"
  Response.Write "ShortPath: " & objFile.ShortPath & "<BR>"
  Response.Write "Created: " & objFile.DateCreated & "&nbsp; &nbsp;"
  Response.Write "LastModified: " & objFile.DateLastModified & "<P>"
Next
JScript程序如下:
//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 + '&nbsp; &nbsp;');
  Response.Write('ShortName: ' + objFile.ShortName + '&nbsp; &nbsp;');
  Response.Write('Size: ' + objFile.Size + ' bytes &nbsp; &nbsp;');
  Response.Write('Type: ' + objFile.Type + '<BR>');
  Response.Write('Path: ' + objFile.Path + '&nbsp; &nbsp;');
  Response.Write('ShortPath: ' + objFile.ShortPath + '<BR>');
  Response.Write('Created: ' + objFile.DateCreated + '&nbsp; &nbsp;');
  Response.Write('Accessed: ' + objFile.DateLastAccessed + '&nbsp; &nbsp;');
  Response.Write('Modified: ' + objFile.DateLastModified + '<P>');
}
该VBScript程序在服务器上运行时的结果如图5-12所示。该页面为folderscollection_vb.asp,来自本书提供的示例文件。

图5-12  Folders集合的内容
(3)    使用特殊文件夹
GetSpecialFolder是FileSystemObject对象的方法之一,它返回计算机上三个“特殊文件夹”对应的Folder对象:
· WindowsFolder:%Windows%目录,缺省为WinNT(或Windows,在非NT/2000计算机上)目录。
· SystemFolder:%System%目录,缺省为WinNT\System32(或Windows\System,在非NT/2000计算机上)目录。
· TemporaryFolder:%Temp%目录,缺省为WinNT\Temp(或Windows\Temp,在非NT/2000计算机上)目录。
为得到对特殊文件夹的引用,我们提供相应的预定义常数作为GetSpecialFolder方法的参数:
' 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>"
或用JScript:
// In JScript:
var objFSO = Server.CreateObject('Scripting.FileSystemObject');

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

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

var objFolder = objFSO.GetSpecialFolder(TemporaryFolder);
Response.Write('GetSpecialFolder(TemporaryFolder) returned - &nbsp;');
Response.Write('Path: ' + objFolder.Path + '&nbsp; &nbsp;');
Response.Write('Type: ' + objFolder.Type + '<BR>');
该VBScript程序在服务器上运行时的结果如图5-13所示。该页面名为specialfolder_vb.asp,来自本书提供的示例文件。

图5-13  GetSpecialFolder方法的使用结果
2.  File对象
File对象提供了对文件的属性的访问,通过它的方法能够对文件进行操作。每个Folder对象提供了一个Files集合,包含文件夹中文件对应的File对象。还可以直接地从FileSystemObject对象中通过使用GetFile方法得到一个File对象引用。
(1)      File对象的属性
File对象有一系列的属性,类似于Folder对象的属性,如表5-11所示:
表5-11  File对象的属性及说明
属 性
说 明

Attributes
返回文件的属性。可以是下列值中的一个或其组合:Normal(0)、ReadOnly(1)、Hidden(2)、System(4)、Volume(名称)(9)、Directory(文件夹)(16)、Archive(32)、Alias(64)和Compressed(128)

DateCreated
返回该文件夹的创建日期和时间

DateLastAccessed
返回最后一次访问该文件的日期和时间

DateLastModified
返回最后一次修改该文件的日期和时间

Drive
返回该文件所在的驱动器的Drive对象

Name
设定或返回文件的名字

ParentFolder
返回该文件的父文件夹的Folder对象

Path
返回文件的绝对路径,可使用长文件名

ShortName
返回DOS风格的8.3形式的文件名

ShortPath
返回DOS风格的8.3形式的文件绝对路径

Size
返回该文件的大小(字节)

Type
如果可能,返回一个文件类型的说明字符串(例如:“Text Document”表示.txt文件)

       (2)  File对象的方法
       同样类似于Folder对象,File对象的方法允许复制、删除以及移动文件。它也有一个使用文本流打开文件的方法。File对象的方法及说明如表5-12所示:
表5-12  File对象的方法及说明
方 法
说 明

Copy(destination,overwrite)
将这个文件复制到destination指定的文件夹。如果destination的末尾是路径分隔符(‘\’),那么认为destination是放置拷贝文件的文件夹。否则认为destination是要创建的新文件的路径和名字。如果目标文件已经存在且overwrite参数设置为False,将产生错误,缺省的overwrite参数是True

Delete(force)
删除这个文件。如果可选的force参数设置为True,即使文件具有只读属性也会被删除。缺省的force是False

Move(destination)
将文件移动到destination指定的文件夹。如果destination的末尾是路径分隔符(‘\’),那么认为destination是一文件夹。否则认为destination是一个新的文件的路径和名字。如果目标文件夹已经存在,则出错

CreateTextFile
(filename,overwrite,unicode)
用指定的文件名创建一个新的文本文件,并且返回一个相应的TextStream对象。如果可选的overwrite参数设置为True,将覆盖任何已有的同名文件。缺省的overwrite参数是False。如果可选的unicode参数设置为True,文件的内容将存储为unicode文本。缺省的unicode是False

OpenAsTextStream
(iomode,format)
打开指定文件并且返回一个TextStream对象,用于文件的读、写或追加。iomode参数指定了要求的访问类型,允许值是ForReading(1) (缺省值)、ForWrite(2)、ForAppending(8)。format参数说明了读、写文件的数据格式。允许值是TristateFalse(0)(缺省),说明用ASCII数据格式;TristateTrue(-1)说明用Unicode数据格式;TristateUseDefault(-2)说明使用系统缺省格式

       因此给定一个File对象后,可以使用ParentFolder属性得到包含该文件的Folder对象的引用,用来在文件系统中导航。甚至可以用Drive属性获得相应的Drive对象的引用,并得到各种Folder对象以及所包含的File对象。
       另外,给定一个Folder对象以及对应的Files集合后,可以通过遍历该集合检查这一文件夹中的每个文件。还可以使用File对象的各种方法以一定方式处理该文件,如复制、移动或删除。下面的代码给出了C驱动器的第一个文件夹的文件列表:
       ' 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 & "&nbsp; &nbsp;"
  Response.Write "ShortName: " & objFile.ShortName & "&nbsp; &nbsp;"
  Response.Write "Size: " & objFile.Size & " bytes &nbsp; &nbsp;"
  Response.Write "Type: " & objFile.Type & "<BR>"
  Response.Write "Path: " & objFile.Path & "&nbsp; &nbsp;"
  Response.Write "ShortPath: " & objFile.ShortPath & "<BR>"
  Response.Write "Created: " & objFile.DateCreated & "&nbsp; &nbsp;"
  Response.Write "LastModified: " & objFile.DateLastModified & "<P>"
Next
注意,不能使用数字索引来定位Folders或Files集合里的条目,因此必须使用For Each … Next语句遍历该集合直到最初的条目,然后使用该条目的Name属性。也不得不使用嵌套的圆括号强迫其作为值(字符串)传送给该Folders集合的Item方法。
用下面的JScript程序可完成同样的工作:
// 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 + '&nbsp; &nbsp;');
  Response.Write('ShortName: ' + objFile.ShortName + '&nbsp; &nbsp;');
  Response.Write('Size: ' + objFile.Size + ' bytes &nbsp; &nbsp;');
  Response.Write('Type: ' + objFile.Type + '<BR>');
  Response.Write('Path: ' + objFile.Path + '&nbsp; &nbsp;');
  Response.Write('ShortPath: ' + objFile.ShortPath + '<BR>');
  Response.Write('Created: ' + objFile.DateCreated + '&nbsp; &nbsp;');
  Response.Write('Accessed: ' + objFile.DateLastAccessed + '&nbsp; &nbsp;');
  Response.Write('Modified: ' + objFile.DateLastModified + '<P>');
}
两个程序的结果是相同的,如图5-14所示。该页面为filescollection_vb.asp,来自本书提供的示例文件。

图5-14  File集合的内容

5.5 Scripting.TextStream对象
       FileSystemObject、Folder和File对象的一些方法都与通过TextStream对象创建、读取或写入文件有关。
虽然TextStream对象定义为FileSystemObject对象的一个独立的附属对象,但我们不得不使用FileSystemObject对象或其附属对象来创建一个TextStream对象并访问磁盘文件的内容。