Scripting.FileSystemObject对象

 
Scripting.FileSystemObject 对象
 
    FileSystemObject 对象提供对计算机文件系统的访问,它允许我们在代码内操作文本文件、文件夹及驱动器。它是脚本运行期库提供的对象之一,对于服务器 ASP 页面内的 VBScript JScript 都有效。如果页面的扩展名为 .hta (表示它们是 HTA 的一部分),它也可用在客户端的 IE 5 中。本节仅讨论在服务器上的 ASP 脚本如何使用 FileSystemObject 对象。
      
超级文本应用程序( HTA )由指定的 受信任的 页面组成,在页面的 <HEAD> 段里包含 <HTA: APPLICATION> 元素。例如:
    <HTA:APPLICATION ID=”objMyApp” APPLICATIONNAME=”myApp”>
这些页面可以使用客户端脚本引擎中的一些不常用特性,这些特性中有 FileSystemObject 对象和 TextStream 对象。关于超级文本应用程序的更多信息,请访问 Microsoft Workshop 网站。
可以使用下面的程序创建一个 FileSystemObject 对象实例:
‘ In vbscript:
Dim objMyFSO
Set objMyFSO = Server.CreateObject(“Scripting.FileSystemObject”)

// In jscript:
var objMyFSO = Server.CreateObject(‘Scripting.FileSystemObject’);

<!-- Server-side with an OBJECT element -->
<OBJECT RUNAT=”SERVER” SCOPE=”PAGE” ID=”objFSO”
              PROGID=”Scripting.FileSystemObject”>
</OBJECT>
ASP 页面里,增加一个对于 FileSystemObject 类型库的引用是非常有用的。这允许使用它直接定义的内置常数,不用像过去那样用数字等效表达式代替。整个脚本运行期库的类型库可以增加到任何 ASP 页面中,代码如下:
<!-- METADATA TYPE=”typelib” FILE=”C:WinNTSystem32scrrun.dll” -->
如果你是在另一个目录下安装 Windows ,必须编辑 FILE 的属性值。
5.4.1 FileSystemObject
对象成员概要
       FileSystemObject
对象提供一个属性和一系列方法,可用它们来操纵 FileSystemObject 对象实现的一些从属对象。这里提供了全部的内容概要,然后介绍每一个从属对象。
1
  FileSystemObject 的属性
FileSystemObject
对象只有一个属性,它用于得到当前机器上的所有有效驱动器的列表,如表 5-4 所示:
5-4  FileSystemObject 对象的属性及说明



Drivers
返回本地计算机可用的驱动器列表。

2
  FileSystemObject 的方法
FileSystemObject
对象提供了使用从属对象的一系列方法,从属对象包括 Drive Folder File 等对象。它也实现了用于 TextStream 对象的两个方法: CreateTextFile OpenTextFile 。根据所使用的对象的类型,将方法划分为三类。
(1)   
与驱动器有关的方法
与驱动器有关的方法如表 5-5 所示:
5-5 与驱动器有关的方法及说明



DriveExists(drivespec)
如果在 drivespec 中指定的驱动器存在,则返回 True ,否则返回 False drivespec 参数可以是一个驱动器字母,或者是文件、文件夹的完整绝对路径

GetDrive(drivespec)
返回 drivespec 指定的驱动器所对应的 Drive 对象。 drivespec 可以包含冒号、路径分隔符或者是网络共享名,即: “C” “C:” “C:” “/machinesharename”

GetDriveName(drivespec)
用字符串返回 drivespec 指定的驱动器的名称。 drivespec 参数必须是文件或文件夹的绝对路径,或者仅仅是驱动器字母,例如: “c:” “c”

(2)   
与文件夹有关的方法
与文件夹有关的方法如表 5-6 所示:
5-6   与文件夹有关的方法及说明



BuildPath(path,name)
在已有的路径 path 上增添名字为 name 的文件或文件夹,如果需要,则增添路径分隔符 ’’

CopyFolder(source,destination,overwrite)
从指定的源文件夹 source (可以包含通配符)中复制一个或多个文件夹到指定的目标文件夹 destination ,包含了源文件夹中的所有文件。如果 source 包含通配符或 destination 末尾是路径分隔符 (‘’) ,那么认为 destination 是要放置源文件夹的拷贝的文件夹。否则的话,认为 destination 是要创建的新文件夹的路径名。如果 destination 文件夹已经存在且 overwrite 参数设置为 False ,将产生错误,缺省的 overwrite 参数是 True

CreateFolder(foldername)
创建一个路径名为 foldername 的文件夹。如果 foldername 已经存在将产生错误

DeleteFolder(folderspec,force)
删除由 folderspec 指定的一个或多个文件夹(可以在路径的最后部分包含通过配符)及文件夹中的所有内容。如果可选的 force 参数设置为 true ,那么即使文件夹包含的文件具有只读属性,也将删除该文件夹。缺省的 force 参数是 False

FolderExist(folderspec)
如果 folderspec 指定的文件夹存在则返回 True ,否则返回 False folderspec 参数可以包含文件夹的绝对或相对路径,或者仅仅是当前文件夹中看到的文件夹名

GetAbsolutePathName(pathspec)
返回明确指定文件夹的路径,其中要考虑到当前文件夹的路径。例如,如果当前文件夹是 “c:docssales” ,而 pathspec “jan” ,返回的字符是 “c:docssalesjan” 。通配符、 ”..” ”/” 路径操作符都是可以接受的

GetFolder(folderspec)
返回 folderspec 指定的文件夹对应的 Folder 对象。 folderspec 可以是文件夹的相对的或绝对的路径

GetParentFolderName(pathspec)
返回 pathspec 文件或文件夹的上一级文件夹。不检验该文件夹是否存在

GetSpecialfolder(folderspec)
返回一个特定的 Windows 文件夹相对应的 Folder 对象。参数 folderspec 的允许值是 WindowsFolder(0) SystemFolder(1) TemporaryFolder(2)

MoveFolder(source,destination)
source 指定的一个或多个文件夹移动到 destination 指定的文件夹。在 source 里可以包含通配符,但在 destination 中不行。如果 source 包含通配符或 destination 末尾是路径分隔符 (‘’) ,则认为 destination 是要放置源文件夹的文件夹,否则认为它是一个新文件夹的完整路径和名字。如果目的文件夹 destination 已经存在则产生错误

(3)   
与文件有关的方法
与文件有关的方法如表 5-7 所示:



CopyFile(source,destination,
overwrite)
source (可包含通配符)指定的一个或多个文件复制到指定的目标文件夹 destination 。如果 source 包含通配符或 destination 末尾是路径分隔符 (‘’) ,那么认为 destination 是文件夹。否则认为 destination 为一新文件的完全路径和名称。如果目标文件夹已经存在且 overwrite 参数设置为 False ,将产生错误。缺省的 overwrite 参数是 True

CreateTextFile(filename,overwrite,
unicode)
用指定的文件名 filename 在磁盘上创建一个新的文本文件,并返回与其对应的 TextStream 对象,如果可选的 overwrite 参数设置为 True ,则覆盖同一路径下已有的同名文件。缺省的 overwrite 参数是 False 。如果可选的 unicode 参数设置为 True ,则该文件的内容将存储为 Unicode 文本,缺省的 unicode 参数是 False

DeleFile(filespec,force)
删除由 filespec 指定的一个或多个文件(可以在路径的最后部分包含通配符)。如果可选的 force 参数设置为 true ,那么也删除具有只读属性的文件。缺省的 force 参数是 False

FileExists(filespec)
如果 filespec 指定的文件存在则返回 True ,否则返回 False filespec 参数可以包含文件的绝对路径或相对路径,或者是当前文件夹中的文件名

GetBaseName(filespec)
返回 filespec 指定的文件的名称,即包含文件路径但去掉了文件的扩展名

GetExtensionName(filespec)
返回 filespec 指定的文件的扩展名

GetFile(filespec)
返回 filespec 指定的文件所对应的 File 对象。可以指定文件的相对或绝对路径

GetFileName(pathspec)
返回 pathspec 指定的文件的路径或文件名,如果没有文件名就返回最后的文件夹名。不检查该文件或文件夹是否存在

GetTempName()
返回一个随机产生的文件名,用于完成运算所需的临时文件或文件夹

MoveFile(source,destination)
source 指定的一个或多个源文件移动到 destination 指定的目的文件夹。在 source 里可以包含通配符,但 destination 不行。如果 source 包含通过配符或 destination 末尾是路径分隔符 (‘’) ,那么认为 destination 是一文件夹。否则,认为 destination 是一新文件夹的完整路径和名称。如果目的文件夹已经存在则产生错误

OpenTextFile(filename,iomode,create,
    format)
创建一个名叫做 filename 的文件,或打开一个现有的名为 filename 的文件,并且返回一个与其相关的 TextStream 对象。 filename 参数可以包含绝对或相对路径。 iomode 参数指定了所要求的访问类型。允许的数值是 ForReading(1) (缺省)、 ForWriting(2) ForAppending(8) 。当写入或追加到一个不存在的文件时,如果 create 参数设置为 true ,就将创建一个新文件。缺省的 create 参数是 False format 参数说明对文件读或写的数据格式。允许数值是: TristatetFalse(0) (缺省),按照 ASCII 格式打开; TristatetTrue(-1) ,按照 Unicode 格式打开; TristateDefault(-2) ,用系统缺省格式打开

       Unicode
文件使用两个字节标识每个字符,取消了 ASCII 字符最多 256 个的限制。

5.4.2
使用驱动器
      
下面是使用 FileSystemObject 对象的简单例子,它使用 DriveExists 方法得到现有的驱动器字母的列表:
       ‘ 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
      
或用 JScript
       // In Jscript
       var objFSO = Server.CreateObject(‘Scripting.FileSystemObject’);
       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>”);
       }
      
这两个程序段的运行结果是相同的,如图 5-9 所示:
      
这一页面为 driveexists_vb.asp ,由本书的示例文件提供。
1
  Drive 对象
正如已经看到的, FileSystemObject 对象包含一个属性 ——Drives ,它返回一个包括本地计算机上所有可用驱动器的集合。
Drives
集合里的每个条目是一个 Drive 对象。 Drive 对象的属性如表 5-8 所示:
5-8  Drive 对象的属性及说明



AvailableSpave
考虑了帐户定额和 / 或其他限制,返回驱动器上对于该用户可用的空间的大小

DriveLetter
返回驱动器的字母

DriveType
返回驱动器的类型。返回值可以是 Unknown(0) Removeable(1) Fixed(2) Network(3) CDRom(4) RamDisk(5) 。然而需要注意的是当前版本的 scrrun.dll 不支持预定义常数 Network ,必须使用十进制 3 来代替

FileSystem
返回驱动器文件系统的类型。返回值包括 “FAT” “NTFS” “CDFS”

FreeSpace
返回驱动器上可用剩余空间的总量

IsReady
返回一个布尔值表明驱动器是否已准备好

Path
返回一个由驱动器字母和冒号组成的驱动器路径,即 “C:”

RootFolder
返回代表的驱动器根目录文件夹的 Folder 对象

SerialNumber
返回一个用于识别磁盘卷的十进制的序列号

ShareName
如果是一个网络驱动器,返回该驱动器的网络共享名

TotalSize
返回驱动器的总容量(以字节为单位)

VolumeName
设定或返回本地驱动器卷名

      
因此,通过使用 Drives 集合里的 Drive 对象,可以在服务器上产生一个驱动器列表,与通过检查每个可能的驱动器字母来判别驱动器是否存在的方法相比,效率更高。我们也可以得到关于该驱动器的信息。在 VBScript 里,代码如下:
' 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>   "
  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>   "

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>   "
    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   "
Response.Write "FreeSpace: <B>" & FormatNumber( _
objDrive.FreeSpace / 1024, 0) & "</B> KB   "
Response.Write "TotalSize: <B>" & FormatNumber(_
objDrive.TotalSize / 1024, 0) & "</B> KB"
  End if  
  Response.Write "<P>"
  End if
Next
      
注意,不能用预定义常数 Network 比较驱动器的 DriveType 属性,因为(至少在 scrrun.dll 的当前版本中)在类型库中省略了 Network 常数,因此不再作为公用的常数使用。
JScript 中,该程序是:
// In jscript:
// create a FileSystemObject instance
var objFSO = Server.CreateObject('Scripting.FileSystemObject');
// 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><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>');
}
}
      
在系统上运行这段程序以前有一点要注意。如果在 A 驱动器里没有磁盘,或 CD-ROM 驱动器里没有光盘,将得到一个错误提示: “Disk Not Ready” 。除了 DriveLetter 属性和 DriveType 属性外,在使用其他属性和方法前,通过检查每个驱动器的 IsReady 属性,可以保护该页面。
      
当在服务器上运行以上 VBScript 代码时,运行结果如图 5-10 所示。这一页面为 drivescollection_vb.asp ,来自本书提供的示例文件。
2
   文件系统定位
FileSystemObject
的几个方法可用于得到其他对象的引用,因此可以在服务器的文件系统和任何网络驱动器中定位。事实上,在 ASP 代码里使用的所有对象或组件中,除了 ActiveX Data Object 组件, FileSystemObject 对象很可能是最复杂的对象之一。
这种复杂性是由于对如何访问文件系统的不同部分,要求有极高的灵活性。例如,可以从 FileSystemObject 向下通过使用各种从属对象定位一个文件。其过程是从 Drives 集合开始,到一个 Drive 对象,再到驱动器的根 Folder 对象,然后到子 Folder 对象,再到文件夹的 Files 集合,最后到集合内的 File 对象。
另外,如果已知要访问的驱动器、文件夹或文件。可以直接对其使用 GetDrive GetFolder GetSpecialFolder GetFile 方法。图 5-11 有助于理解所有文件系统定位相关的组件、对象、方法和属性之间的关系。
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 & "   "
  Response.Write "ShortPath: " & objFile.ShortPath & "<BR>"
  Response.Write "Created: " & objFile.DateCreated & "   "
  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 + '   ');
  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>');
}
VBScript 程序在服务器上运行时的结果如图 5-12 所示。该页面为 folderscollection_vb.asp ,来自本书提供的示例文件。
(3)   
使用特殊文件夹
GetSpecialFolder
FileSystemObject 对象的方法之一,它返回计算机上三个 特殊文件夹 对应的 Folder 对象:
· WindowsFolder
%Windows% 目录,缺省为 WinNT (或 Windows ,在非 NT/2000 计算机上)目录。
· SystemFolder
%System% 目录,缺省为 WinNTSystem32 (或 WindowsSystem ,在非 NT/2000 计算机上)目录。
· TemporaryFolder
%Temp% 目录,缺省为 WinNTTemp (或 WindowsTemp ,在非 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 -  ');
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>');
VBScript 程序在服务器上运行时的结果如图 5-13 所示。该页面名为 specialfolder_vb.asp ,来自本书提供的示例文件。

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 & "   "
  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
注意,不能使用数字索引来定位 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 + '   ');
  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>');
}
两个程序的结果是相同的,如图 5-14 所示。该页面为 filescollection_vb.asp ,来自本书提供的示例文件。

    
C 驱动器上的第一个文件夹可能是 CAConfig ,缺省情况下该文件夹是空的。这种情况下,可先向该文件夹复制一些文件,完成实验以后再删除这些文件。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值