<p>Scripting.FileSystemObject对象</p> <p>FileSystemObject对象提供对计算机文件系统的访问,它允许我们在代码内操作文本文件、文件夹及驱动器。它是脚本运行期库提供的对象之一,对于服务器ASP页面内的VBScript和JScript都有效。如果页面的扩展名为.hta(表示它们是HTA的一部分),它也可用在客户端的IE 5中。本节仅讨论在服务器上的ASP脚本如何使用FileSystemObject对象。<br /> 超级文本应用程序(HTA)由指定的&ldquo;受信任的&rdquo;页面组成,在页面的&lt;HEAD&gt;段里包含&lt;HTA: APPLICATION&gt;元素。例如:<br /> &lt;HTA:APPLICATION ID=&rdquo;objMyApp&rdquo; APPLICATIONNAME=&rdquo;myApp&rdquo;&gt;<br /> 这些页面可以使用客户端脚本引擎中的一些不常用特性,这些特性中有FileSystemObject对象和TextStream对象。关于超级文本应用程序的更多信息,请访问Microsoft Workshop网站。<br /> 可以使用下面的程序创建一个FileSystemObject对象实例:<br /> &lsquo; In VBScript:<br /> Dim objMyFSO<br /> Set objMyFSO = Server.CreateObject(&ldquo;Scripting.FileSystemObject&rdquo;)</p> <p>// In JScript:<br /> var objMyFSO = Server.CreateObject(&lsquo;Scripting.FileSystemObject&rsquo;);</p> <p>&lt;!-- Server-side with an OBJECT element --&gt;<br /> &lt;OBJECT RUNAT=&rdquo;SERVER&rdquo; SCOPE=&rdquo;PAGE&rdquo; ID=&rdquo;objFSO&rdquo;<br /> PROGID=&rdquo;Scripting.FileSystemObject&rdquo;&gt;<br /> &lt;/OBJECT&gt;<br /> 在ASP页面里,增加一个对于FileSystemObject类型库的引用是非常有用的。这允许使用它直接定义的内置常数,不用像过去那样用数字等效表达式代替。整个脚本运行期库的类型库可以增加到任何ASP页面中,代码如下:<br /> &lt;!-- METADATA TYPE=&rdquo;typelib&rdquo; FILE=&rdquo;C:WinNTSystem32scrrun.dll&rdquo; --&gt;如果你是在另一个目录下安装Windows,必须编辑FILE的属性值。<br /> 5.4.1 FileSystemObject对象成员概要<br /> FileSystemObject对象提供一个属性和一系列方法,可用它们来操纵FileSystemObject对象实现的一些从属对象。这里提供了全部的内容概要,然后介绍每一个从属对象。<br /> 1. FileSystemObject的属性<br /> FileSystemObject对象只有一个属性,它用于得到当前机器上的所有有效驱动器的列表,如表5-4所示:<br /> 表5-4 FileSystemObject对象的属性及说明<br /> 属 性 <br /> 说 明</p> <p>Drivers <br /> 返回本地计算机可用的驱动器列表。</p> <p>2. FileSystemObject的方法<br /> FileSystemObject对象提供了使用从属对象的一系列方法,从属对象包括Drive、Folder和File等对象。它也实现了用于TextStream对象的两个方法:CreateTextFile和OpenTextFile。根据所使用的对象的类型,将方法划分为三类。<br /> (1) 与驱动器有关的方法<br /> 与驱动器有关的方法如表5-5所示:<br /> 表5-5 与驱动器有关的方法及说明<br /> 方 法 <br /> 说 明</p> <p>DriveExists(drivespec) <br /> 如果在drivespec中指定的驱动器存在,则返回True,否则返回False。drivespec参数可以是一个驱动器字母,或者是文件、文件夹的完整绝对路径</p> <p>GetDrive(drivespec) <br /> 返回drivespec指定的驱动器所对应的Drive对象。drivespec可以包含冒号、路径分隔符或者是网络共享名,即:&ldquo;C&rdquo;、&ldquo;C:&rdquo;、&ldquo;C:&rdquo;及&ldquo;\machinesharename&rdquo;</p> <p>GetDriveName(drivespec) <br /> 用字符串返回drivespec指定的驱动器的名称。drivespec参数必须是文件或文件夹的绝对路径,或者仅仅是驱动器字母,例如:&ldquo;c:&rdquo;或&ldquo;c&rdquo;</p> <p>(2) 与文件夹有关的方法<br /> 与文件夹有关的方法如表5-6所示:<br /> 表5-6 与文件夹有关的方法及说明<br /> 方 法 <br /> 说 明</p> <p>BuildPath(path,name) <br /> 在已有的路径path上增添名字为name的文件或文件夹,如果需要,则增添路径分隔符&rsquo;&rsquo;</p> <p>CopyFolder(source,destination,overwrite) <br /> 从指定的源文件夹source(可以包含通配符)中复制一个或多个文件夹到指定的目标文件夹destination,包含了源文件夹中的所有文件。如果source包含通配符或destination末尾是路径分隔符(&lsquo;&rsquo;),那么认为destination是要放置源文件夹的拷贝的文件夹。否则的话,认为destination是要创建的新文件夹的路径名。如果destination文件夹已经存在且overwrite参数设置为False,将产生错误,缺省的overwrite参数是True</p> <p>CreateFolder(foldername) <br /> 创建一个路径名为foldername的文件夹。如果foldername已经存在将产生错误</p> <p>DeleteFolder(folderspec,force) <br /> 删除由folderspec指定的一个或多个文件夹(可以在路径的最后部分包含通过配符)及文件夹中的所有内容。如果可选的force参数设置为true,那么即使文件夹包含的文件具有只读属性,也将删除该文件夹。缺省的force参数是False</p> <p>FolderExist(folderspec) <br /> 如果folderspec指定的文件夹存在则返回True,否则返回False。folderspec参数可以包含文件夹的绝对或相对路径,或者仅仅是当前文件夹中看到的文件夹名</p> <p>GetAbsolutePathName(pathspec) <br /> 返回明确指定文件夹的路径,其中要考虑到当前文件夹的路径。例如,如果当前文件夹是&ldquo;c:docssales&rdquo;,而pathspec是&ldquo;jan&rdquo;,返回的字符是&ldquo;c:docssalesjan&rdquo;。通配符、&rdquo;..&rdquo;和&rdquo;\&rdquo;路径操作符都是可以接受的</p> <p>GetFolder(folderspec) <br /> 返回folderspec指定的文件夹对应的Folder对象。folderspec可以是文件夹的相对的或绝对的路径</p> <p>GetParentFolderName(pathspec) <br /> 返回pathspec文件或文件夹的上一级文件夹。不检验该文件夹是否存在</p> <p>GetSpecialfolder(folderspec) <br /> 返回一个特定的Windows文件夹相对应的Folder对象。参数folderspec的允许值是WindowsFolder(0)、SystemFolder(1)和TemporaryFolder(2)</p> <p>MoveFolder(source,destination) <br /> 将source指定的一个或多个文件夹移动到destination指定的文件夹。在source里可以包含通配符,但在destination中不行。如果source包含通配符或destination末尾是路径分隔符(&lsquo;&rsquo;),则认为destination是要放置源文件夹的文件夹,否则认为它是一个新文件夹的完整路径和名字。如果目的文件夹destination已经存在则产生错误</p> <p>(3) 与文件有关的方法<br /> 与文件有关的方法如表5-7所示:<br /> 方 法 <br /> 说 明</p> <p>CopyFile(source,destination,<br /> overwrite) <br /> 将source(可包含通配符)指定的一个或多个文件复制到指定的目标文件夹destination。如果source包含通配符或destination末尾是路径分隔符(&lsquo;&rsquo;),那么认为destination是文件夹。否则认为destination为一新文件的完全路径和名称。如果目标文件夹已经存在且overwrite参数设置为False,将产生错误。缺省的overwrite参数是True</p> <p>CreateTextFile(filename,overwrite,<br /> unicode) <br /> 用指定的文件名filename在磁盘上创建一个新的文本文件,并返回与其对应的TextStream对象,如果可选的overwrite参数设置为True,则覆盖同一路径下已有的同名文件。缺省的overwrite参数是False。如果可选的unicode参数设置为True,则该文件的内容将存储为Unicode文本,缺省的unicode参数是False</p> <p>DeleFile(filespec,force) <br /> 删除由filespec指定的一个或多个文件(可以在路径的最后部分包含通配符)。如果可选的force参数设置为true,那么也删除具有只读属性的文件。缺省的force参数是False</p> <p>FileExists(filespec) <br /> 如果filespec指定的文件存在则返回True,否则返回False。filespec参数可以包含文件的绝对路径或相对路径,或者是当前文件夹中的文件名</p> <p>GetBaseName(filespec) <br /> 返回filespec指定的文件的名称,即包含文件路径但去掉了文件的扩展名</p> <p>GetExtensionName(filespec) <br /> 返回filespec指定的文件的扩展名</p> <p>GetFile(filespec) <br /> 返回filespec指定的文件所对应的File对象。可以指定文件的相对或绝对路径</p> <p>GetFileName(pathspec) <br /> 返回pathspec指定的文件的路径或文件名,如果没有文件名就返回最后的文件夹名。不检查该文件或文件夹是否存在</p> <p>GetTempName() <br /> 返回一个随机产生的文件名,用于完成运算所需的临时文件或文件夹</p> <p>MoveFile(source,destination) <br /> 将source指定的一个或多个源文件移动到destination指定的目的文件夹。在source里可以包含通配符,但destination不行。如果source包含通过配符或destination末尾是路径分隔符(&lsquo;&rsquo;),那么认为destination是一文件夹。否则,认为destination是一新文件夹的完整路径和名称。如果目的文件夹已经存在则产生错误</p> <p>OpenTextFile(filename,iomode,create,<br /> format) <br /> 创建一个名叫做filename的文件,或打开一个现有的名为filename的文件,并且返回一个与其相关的TextStream对象。filename参数可以包含绝对或相对路径。iomode参数指定了所要求的访问类型。允许的数值是ForReading(1)(缺省)、ForWriting(2)、ForAppending(8)。当写入或追加到一个不存在的文件时,如果create参数设置为true,就将创建一个新文件。缺省的create参数是False。format参数说明对文件读或写的数据格式。允许数值是:TristatetFalse(0)(缺省),按照ASCII格式打开;TristatetTrue(-1),按照Unicode格式打开;TristateDefault(-2),用系统缺省格式打开</p> <p> Unicode文件使用两个字节标识每个字符,取消了ASCII字符最多256个的限制。</p> <p>5.4.2 使用驱动器<br /> 下面是使用FileSystemObject对象的简单例子,它使用DriveExists方法得到现有的驱动器字母的列表:<br /> &lsquo; In VBScript<br /> Set objFSO = Server.CreateObject(&ldquo;Scripting.FileSystemObject&rdquo;)<br /> For intCode = 65 To 90 &lsquo;ANSI codes for &lsquo;A&rsquo; to &lsquo;Z&rsquo;<br /> strLetter = Chr(intCode)<br /> If objFSO.DriveExists(strLetter) Then<br /> Response.Write &ldquo;Found drive &ldquo; &amp; strLetter &amp; &ldquo;:&lt;BR&gt;&rdquo;<br /> End If<br /> Next<br /> 或用JScript:<br /> // In Jscript<br /> var objFSO = Server.CreateObject(&lsquo;Scripting.FileSystemObject&rsquo;);<br /> for (var intCode = 65; intCode &lt;= 90; intCode++) { //ANSI codes for &lsquo;A&rsquo; to &lsquo;Z&rsquo;<br /> strLetter = String.formCharCode(intCode);<br /> If (objFSO.DriveExists(strLetter))<br /> Response.Write (&lsquo;Found drive &lsquo; + strLetter + &ldquo;:&lt;BR&gt;&rdquo;);<br /> }<br /> 这两个程序段的运行结果是相同的,如图5-9所示:</p> <p><br /> 这一页面为driveexists_vb.asp,由本书的示例文件提供。<br /> 1. Drive对象<br /> 正如已经看到的,FileSystemObject对象包含一个属性&mdash;&mdash;Drives,它返回一个包括本地计算机上所有可用驱动器的集合。<br /> Drives集合里的每个条目是一个Drive对象。Drive对象的属性如表5-8所示:<br /> 表5-8 Drive对象的属性及说明<br /> 属 性 <br /> 说 明</p> <p>AvailableSpave <br /> 考虑了帐户定额和/或其他限制,返回驱动器上对于该用户可用的空间的大小</p> <p>DriveLetter <br /> 返回驱动器的字母</p> <p>DriveType <br /> 返回驱动器的类型。返回值可以是Unknown(0)、Removeable(1)、Fixed(2)、Network(3)、CDRom(4)和RamDisk(5)。然而需要注意的是当前版本的scrrun.dll不支持预定义常数Network,必须使用十进制3来代替</p> <p>FileSystem <br /> 返回驱动器文件系统的类型。返回值包括&ldquo;FAT&rdquo;、&ldquo;NTFS&rdquo;和&ldquo;CDFS&rdquo;</p> <p>FreeSpace <br /> 返回驱动器上可用剩余空间的总量</p> <p>IsReady <br /> 返回一个布尔值表明驱动器是否已准备好</p> <p>Path <br /> 返回一个由驱动器字母和冒号组成的驱动器路径,即&ldquo;C:&rdquo;</p> <p>RootFolder <br /> 返回代表的驱动器根目录文件夹的Folder对象</p> <p>SerialNumber <br /> 返回一个用于识别磁盘卷的十进制的序列号</p> <p>ShareName <br /> 如果是一个网络驱动器,返回该驱动器的网络共享名</p> <p>TotalSize <br /> 返回驱动器的总容量(以字节为单位)</p> <p>VolumeName <br /> 设定或返回本地驱动器卷名</p> <p> 因此,通过使用Drives集合里的Drive对象,可以在服务器上产生一个驱动器列表,与通过检查每个可能的驱动器字母来判别驱动器是否存在的方法相比,效率更高。我们也可以得到关于该驱动器的信息。在VBScript里,代码如下:<br /> ' In VBScript:<br /> ' create a FileSystemObject instance<br /> Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)<br /> ' create a Drives collection<br /> Set colDrives = objFSO.Drives<br /> ' iterate through the Drives collection<br /> For Each objDrive in colDrives</p> <p> Response.Write &quot;DriveLetter: &lt;B&gt;&quot; &amp; objDrive.DriveLetter &amp; &quot;&lt;/B&gt; &quot;<br /> Response.Write &quot;DriveType: &lt;B&gt;&quot; &amp; objDrive.DriveType<br /> Select Case objDrive.DriveType<br /> Case 0: Response.Write &quot; - (Unknown)&quot;<br /> Case 1: Response.Write &quot; - (Removable)&quot;<br /> Case 2: Response.Write &quot; - (Fixed)&quot;<br /> Case 3: Response.Write &quot; - (Network)&quot;<br /> Case 4: Response.Write &quot; - (CDRom)&quot;<br /> Case 5: Response.Write &quot; - (RamDisk)&quot;<br /> End Select<br /> Response.Write &quot;&lt;/B&gt; &quot;</p> <p>If objDrive.DriveType = 3 Then<br /> If objDrive.IsReady Then<br /> Response.Write &quot;Remote drive with ShareName: &lt;B&gt;&quot; &amp; objDrive.ShareName &amp; &quot;&lt;/B&gt;&quot;<br /> Else<br /> Response.Write &quot;Remote drive - &lt;B&gt;IsReady&lt;/B&gt; property returned_<br /> &lt;B&gt;False&lt;/B&gt;&lt;BR&gt;&quot;<br /> End If<br /> Else If objDrive.IsReady then <br /> Response.Write &quot;FileSystem: &lt;B&gt;&quot; &amp; objDrive.FileSystem &amp; &quot;&lt;/B&gt; &quot;<br /> Response.Write &quot;SerialNumber: &lt;B&gt;&quot; &amp; objDrive.SerialNumber &amp; &quot;&lt;/B&gt;&lt;BR&gt;&quot;<br /> Response.Write &quot;Local drive with VolumeName: &lt;B&gt;&quot; &amp; _<br /> objDrive.VolumeName &amp; &quot;&lt;/B&gt;&lt;BR&gt;&quot;<br /> Response.Write &quot;AvailableSpace: &lt;B&gt;&quot; &amp; FormatNumber( _<br /> objDrive.AvailableSpace / 1024, 0) &amp; &quot;&lt;/B&gt; KB &quot;<br /> Response.Write &quot;FreeSpace: &lt;B&gt;&quot; &amp; FormatNumber( _<br /> objDrive.FreeSpace / 1024, 0) &amp; &quot;&lt;/B&gt; KB &quot;<br /> Response.Write &quot;TotalSize: &lt;B&gt;&quot; &amp; FormatNumber(_ <br /> objDrive.TotalSize / 1024, 0) &amp; &quot;&lt;/B&gt; KB&quot;<br /> End if <br /> Response.Write &quot;&lt;P&gt;&quot;<br /> End if<br /> Next<br /> 注意,不能用预定义常数Network比较驱动器的DriveType属性,因为(至少在scrrun.dll的当前版本中)在类型库中省略了Network常数,因此不再作为公用的常数使用。<br /> 在JScript中,该程序是:<br /> // In JScript:<br /> // create a FileSystemObject instance<br /> var objFSO = Server.CreateObject('Scripting.FileSystemObject');<br /> // create a Drives collection<br /> var colDrives = new Enumerator(objFSO.Drives);</p> <p>for (; !colDrives.atEnd(); colDrives.moveNext()) {<br /> objDrive = colDrives.item();<br /> Response.Write('DriveLetter: ' + objDrive.DriveLetter + '&lt;BR&gt;');<br /> Response.Write('DriveType: ' + objDrive.DriveType + '&lt;BR&gt;');</p> <p> if (objDrive.DriveType == 3)<br /> if (objDrive.IsReady) <br /> Response.Write('Remote drive with ShareName: ' + <br /> objDrive.ShareName + '&lt;BR&gt;')<br /> else<br /> Response.Write('Remote drive - IsReady property returned False&lt;BR&gt;&lt;BR&gt;');<br /> else if (objDrive.IsReady) {<br /> Response.Write('Local drive with VolumeName: ' + <br /> objDrive.VolumeName + '&lt;BR&gt;');<br /> Response.Write('FileSystem: ' + objDrive.FileSystem + '&lt;BR&gt;');<br /> Response.Write('SerialNumber: ' + objDrive.SerialNumber + '&lt;BR&gt;');<br /> Response.Write('AvailableSpace: ' + objDrive.AvailableSpace + ' bytes&lt;BR&gt;');<br /> Response.Write('FreeSpace: ' + objDrive.FreeSpace + ' bytes&lt;BR&gt;');<br /> Response.Write('TotalSize: ' + objDrive.TotalSize + ' bytes&lt;P&gt;');<br /> }<br /> }<br /> 在系统上运行这段程序以前有一点要注意。如果在A驱动器里没有磁盘,或CD-ROM驱动器里没有光盘,将得到一个错误提示:&ldquo;Disk Not Ready&rdquo;。除了DriveLetter属性和DriveType属性外,在使用其他属性和方法前,通过检查每个驱动器的IsReady属性,可以保护该页面。<br /> 当在服务器上运行以上VBScript代码时,运行结果如图5-10所示。这一页面为drivescollection_vb.asp,来自本书提供的示例文件。</p> <p>2. 文件系统定位<br /> FileSystemObject的几个方法可用于得到其他对象的引用,因此可以在服务器的文件系统和任何网络驱动器中定位。事实上,在ASP代码里使用的所有对象或组件中,除了ActiveX Data Object组件,FileSystemObject对象很可能是最复杂的对象之一。<br /> 这种复杂性是由于对如何访问文件系统的不同部分,要求有极高的灵活性。例如,可以从FileSystemObject向下通过使用各种从属对象定位一个文件。其过程是从Drives集合开始,到一个Drive对象,再到驱动器的根Folder对象,然后到子Folder对象,再到文件夹的Files集合,最后到集合内的File对象。<br /> 另外,如果已知要访问的驱动器、文件夹或文件。可以直接对其使用GetDrive、GetFolder、GetSpecialFolder和GetFile方法。图5-11有助于理解所有文件系统定位相关的组件、对象、方法和属性之间的关系。</p> <p>1. Folder对象<br /> Driver对象的RootFolder属性返回一个Folder对象,通过该对象可访问这个驱动器内的所有的内容。可以使用这个Folder对象的属性和方法遍历驱动器上的目录,并得到该文件夹和其他文件夹的属性。<br /> (1) Folder对象的属性<br /> Folder对象提供一组属性,可用这些属性得到关于当前文件夹的更多信息,也可以改变该文件夹的名称。其属性及说明如表5-9所示:<br /> 表5-9 Folder 对象的属性及说明<br /> 属 性 <br /> 说 明</p> <p>Attributes <br /> 返回文件夹的属性。可以是下列值中的一个或其组合:Normal(0)、ReadOnly(1)、Hidden(2)、System(4)、Volume(名称)(8)、Directory(文件夹)(16)、Archive(32)、Alias(64)和Compressed(128)。例如,一个隐藏的只读文件,Attributes的值为3</p> <p>DateCreated <br /> 返回该文件夹的创建日期和时间</p> <p>DateLastAccessed <br /> 返回最后一次访问该文件夹的日期和时间</p> <p>DateLastModified <br /> 返回最后一次修改该文件夹的日期和时间</p> <p>Drive <br /> 返回该文件夹所在的驱动器的驱动器字母</p> <p>Files <br /> 返回Folder对象包含的Files集合,表示该文件夹内所有的文件</p> <p>IsRootFolder <br /> 返回一个布尔值说明该文件夹是否是当前驱动器的根文件夹</p> <p>Name <br /> 设定或返回文件夹的名字</p> <p>ParentFolder <br /> 返回该文件夹的父文件夹对应的Folder对象</p> <p>Path <br /> 返回文件夹的绝对路径,使用相应的长文件名</p> <p>ShortName <br /> 返回DOS风格的8.3形式的文件夹名</p> <p>ShortPath <br /> 返回DOS风格的8.3形式的文件夹的绝对路径</p> <p>Size <br /> 返回包含在该文件夹里所有文件和子文件夹的大小</p> <p>SubFolers <br /> 返回该文件夹内包含的所有子文件夹对应的Folders集合,包括隐藏文件夹和系统文件夹</p> <p>Type <br /> 如果可能,返回一个文件夹的说明字符串(例如,&ldquo;Recycle Bin&rdquo;)</p> <p>(2) Folder对象的方法<br /> Folder对象提供一组可用于复制、删除和移动当前文件夹的方法。这些方法的运行方式与FileSystemObject对象的CopyFolder、DeleFolder和MoveFolder方法相同,但这些方法不要求source参数,因为源文件就是这个文件夹。这些方法及说明如表5-10所示:<br /> 表5-10 Folder对象的方法及说明<br /> 方 法 <br /> 说 明</p> <p>Copy(destination,overwrite) <br /> 将这个文件夹及所有的内容复制到destination指定的文件夹。如果destination的末尾是路径分隔符(&lsquo;&rsquo;),那么认为destination是放置拷贝文件夹的一个文件夹。否则认为destination是要创建的新文件夹的路径和名字。如果目标文件夹已经存在且overwrite参数设置为False,将产生错误,缺省的overwrite参数是True</p> <p>Delete(force) <br /> 删除文件夹及里面的所有内容。如果可选的force参数设置为True,即使文件夹设置为只读或含有只读的文件,也将删除该文件夹。缺省的force是False</p> <p>Move(destination) <br /> 将文件夹及里面所有的内容移动到destination指定的文件夹。如果destination的末尾是路径分隔符(&lsquo;&rsquo;),那么认为destination是放置移动文件夹的一个文件夹。否则认为destination是一个新的文件夹的路径和名字。如果目标文件夹已经存在,则出错</p> <p>CreateTextFile<br /> (filename,overwrite,unicode) <br /> 用指定的文件名在文件夹内创建一个新的文本文件,并且返回一个相应的TextStream对象。如果可选的overwrite参数设置为True,将覆盖任何已有的同名文件。缺省的overwrite参数是False。如果可选的unicode参数设置为True,文件的内容将存储为unicode文本。缺省的unicode是False</p> <p> 在文件夹之间可以使用当前文件夹的ParentFolder属性,返回到父目录。当到达一个文件夹时,如果IsRootFolder属性是True,就停下来。离开驱动器的根目录,沿目录树向下,可遍历或访问在Folders集合(由当前文件夹的SubFolders属性返回)内的指定文件夹。<br /> 下列程序遍历了驱动器C根目录内的所有文件夹,并显示各个文件夹的有关信息。<br /> VBScript程序如下:<br /> 'In VBScript:<br /> ' Create a FileSystemObject instance<br /> Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)<br /> ' Get a reference to drive C<br /> Set objDriveC = objFSO.GetDrive(&quot;C:&quot;)<br /> ' Get a reference to the root folder<br /> Set objRoot = objDriveC.RootFolder<br /> ' Get a reference to the SubFolders collection<br /> Set objFolders = objRoot.SubFolders<br /> ' Get a reference to the first folder in the SubFolders collection<br /> For Each objFolder In objFolders<br /> Set objFolder1 = objFolders.Item((objFolder.Name))<br /> Exit For<br /> Next<br /> ' Iterate through all the files in this folder<br /> For Each objFile in objFolder1.Files<br /> Response.Write &quot;Name: &quot; &amp; objFile.Name &amp; &quot; &quot;<br /> Response.Write &quot;ShortName: &quot; &amp; objFile.ShortName &amp; &quot; &quot;<br /> Response.Write &quot;Size: &quot; &amp; objFile.Size &amp; &quot; bytes &quot;<br /> Response.Write &quot;Type: &quot; &amp; objFile.Type &amp; &quot;&lt;BR&gt;&quot;<br /> Response.Write &quot;Path: &quot; &amp; objFile.Path &amp; &quot; &quot;<br /> Response.Write &quot;ShortPath: &quot; &amp; objFile.ShortPath &amp; &quot;&lt;BR&gt;&quot;<br /> Response.Write &quot;Created: &quot; &amp; objFile.DateCreated &amp; &quot; &quot;<br /> Response.Write &quot;LastModified: &quot; &amp; objFile.DateLastModified &amp; &quot;&lt;P&gt;&quot;<br /> Next<br /> JScript程序如下:<br /> //In JScript:<br /> // Create a FileSystemObject instance<br /> var objFSO = Server.CreateObject('Scripting.FileSystemObject');<br /> // Get a reference to drive C<br /> var objDriveC = objFSO.GetDrive('C:');<br /> // Get a reference to the root folder<br /> var objRoot = objDriveC.RootFolder;<br /> // Get a reference to the first folder in the SubFolders collection<br /> var colAllFolders = new Enumerator(objRoot.SubFolders);<br /> var objFolder1 = colAllFolders.item();<br /> // Get a reference to the Files collection for this folder<br /> var colFiles = new Enumerator(objFolder1.Files);</p> <p>// Iterate through all the files in this collection<br /> for (; !colFiles.atEnd(); colFiles.moveNext()) {<br /> objFile = colFiles.item()<br /> Response.Write('Name: ' + objFile.Name + ' ');<br /> Response.Write('ShortName: ' + objFile.ShortName + ' ');<br /> Response.Write('Size: ' + objFile.Size + ' bytes ');<br /> Response.Write('Type: ' + objFile.Type + '&lt;BR&gt;');<br /> Response.Write('Path: ' + objFile.Path + ' ');<br /> Response.Write('ShortPath: ' + objFile.ShortPath + '&lt;BR&gt;');<br /> Response.Write('Created: ' + objFile.DateCreated + ' ');<br /> Response.Write('Accessed: ' + objFile.DateLastAccessed + ' ');<br /> Response.Write('Modified: ' + objFile.DateLastModified + '&lt;P&gt;');<br /> }<br /> 该VBScript程序在服务器上运行时的结果如图5-12所示。该页面为folderscollection_vb.asp,来自本书提供的示例文件。</p> <p>(3) 使用特殊文件夹<br /> GetSpecialFolder是FileSystemObject对象的方法之一,它返回计算机上三个&ldquo;特殊文件夹&rdquo;对应的Folder对象:<br /> &middot; WindowsFolder:%Windows%目录,缺省为WinNT(或Windows,在非NT/2000计算机上)目录。<br /> &middot; SystemFolder:%System%目录,缺省为WinNTSystem32(或WindowsSystem,在非NT/2000计算机上)目录。<br /> &middot; TemporaryFolder:%Temp%目录,缺省为WinNTTemp(或WindowsTemp,在非NT/2000计算机上)目录。<br /> 为得到对特殊文件夹的引用,我们提供相应的预定义常数作为GetSpecialFolder方法的参数:<br /> ' In VBScript:<br /> Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)</p> <p>Set objFolder = objFSO.GetSpecialFolder(WindowsFolder)<br /> Response.Write &quot;GetSpecialFolder(WindowsFolder) returned:&lt;BR&gt;&quot;<br /> Response.Write &quot;Path: &quot; &amp; objFolder.Path &amp; &quot;&lt;BR&gt;&quot;<br /> Response.Write &quot;Type: &quot; &amp; objFolder.Type &amp; &quot;&lt;P&gt;&quot;</p> <p>Set objFolder = objFSO.GetSpecialFolder(SystemFolder)<br /> Response.Write &quot;GetSpecialFolder(SystemFolder) returned:&lt;BR&gt;&quot;<br /> Response.Write &quot;Path: &quot; &amp; objFolder.Path &amp; &quot;&lt;BR&gt;&quot;<br /> Response.Write &quot;Type: &quot; &amp; objFolder.Type &amp; &quot;&lt;P&gt;&quot;</p> <p>Set objFolder = objFSO.GetSpecialFolder(TemporaryFolder)<br /> Response.Write &quot;GetSpecialFolder(TemporaryFolder) returned:&lt;BR&gt;&quot;<br /> Response.Write &quot;Path: &quot; &amp; objFolder.Path &amp; &quot;&lt;BR&gt;&quot;<br /> Response.Write &quot;Type: &quot; &amp; objFolder.Type &amp; &quot;&lt;P&gt;&quot;<br /> 或用JScript:<br /> // In JScript:<br /> var objFSO = Server.CreateObject('Scripting.FileSystemObject');</p> <p>var objFolder = objFSO.GetSpecialFolder(WindowsFolder);<br /> Response.Write('GetSpecialFolder(WindowsFolder) returned - ');<br /> Response.Write('Path: ' + objFolder.Path + ' ');<br /> Response.Write('Type: ' + objFolder.Type + '&lt;BR&gt;');</p> <p>var objFolder = objFSO.GetSpecialFolder(SystemFolder);<br /> Response.Write('GetSpecialFolder(SystemFolder) returned - ');<br /> Response.Write('Path: ' + objFolder.Path + ' ');<br /> Response.Write('Type: ' + objFolder.Type + '&lt;BR&gt;');</p> <p>var objFolder = objFSO.GetSpecialFolder(TemporaryFolder);<br /> Response.Write('GetSpecialFolder(TemporaryFolder) returned - ');<br /> Response.Write('Path: ' + objFolder.Path + ' ');<br /> Response.Write('Type: ' + objFolder.Type + '&lt;BR&gt;');<br /> 该VBScript程序在服务器上运行时的结果如图5-13所示。该页面名为specialfolder_vb.asp,来自本书提供的示例文件。</p> <p>2. File对象<br /> File对象提供了对文件的属性的访问,通过它的方法能够对文件进行操作。每个Folder对象提供了一个Files集合,包含文件夹中文件对应的File对象。还可以直接地从FileSystemObject对象中通过使用GetFile方法得到一个File对象引用。<br /> (1) File对象的属性<br /> File对象有一系列的属性,类似于Folder对象的属性,如表5-11所示:<br /> 表5-11 File对象的属性及说明<br /> 属 性 <br /> 说 明</p> <p>Attributes <br /> 返回文件的属性。可以是下列值中的一个或其组合:Normal(0)、ReadOnly(1)、Hidden(2)、System(4)、Volume(名称)(9)、Directory(文件夹)(16)、Archive(32)、Alias(64)和Compressed(128)</p> <p>DateCreated <br /> 返回该文件夹的创建日期和时间</p> <p>DateLastAccessed <br /> 返回最后一次访问该文件的日期和时间</p> <p>DateLastModified <br /> 返回最后一次修改该文件的日期和时间</p> <p>Drive <br /> 返回该文件所在的驱动器的Drive对象</p> <p>Name <br /> 设定或返回文件的名字</p> <p>ParentFolder <br /> 返回该文件的父文件夹的Folder对象</p> <p>Path <br /> 返回文件的绝对路径,可使用长文件名</p> <p>ShortName <br /> 返回DOS风格的8.3形式的文件名</p> <p>ShortPath <br /> 返回DOS风格的8.3形式的文件绝对路径</p> <p>Size <br /> 返回该文件的大小(字节)</p> <p>Type <br /> 如果可能,返回一个文件类型的说明字符串(例如:&ldquo;Text Document&rdquo;表示.txt文件)</p> <p> (2) File对象的方法<br /> 同样类似于Folder对象,File对象的方法允许复制、删除以及移动文件。它也有一个使用文本流打开文件的方法。File对象的方法及说明如表5-12所示:<br /> 表5-12 File对象的方法及说明<br /> 方 法 <br /> 说 明</p> <p>Copy(destination,overwrite) <br /> 将这个文件复制到destination指定的文件夹。如果destination的末尾是路径分隔符(&lsquo;&rsquo;),那么认为destination是放置拷贝文件的文件夹。否则认为destination是要创建的新文件的路径和名字。如果目标文件已经存在且overwrite参数设置为False,将产生错误,缺省的overwrite参数是True</p> <p>Delete(force) <br /> 删除这个文件。如果可选的force参数设置为True,即使文件具有只读属性也会被删除。缺省的force是False</p> <p>Move(destination) <br /> 将文件移动到destination指定的文件夹。如果destination的末尾是路径分隔符(&lsquo;&rsquo;),那么认为destination是一文件夹。否则认为destination是一个新的文件的路径和名字。如果目标文件夹已经存在,则出错</p> <p>CreateTextFile<br /> (filename,overwrite,unicode) <br /> 用指定的文件名创建一个新的文本文件,并且返回一个相应的TextStream对象。如果可选的overwrite参数设置为True,将覆盖任何已有的同名文件。缺省的overwrite参数是False。如果可选的unicode参数设置为True,文件的内容将存储为unicode文本。缺省的unicode是False</p> <p>OpenAsTextStream<br /> (iomode,format) <br /> 打开指定文件并且返回一个TextStream对象,用于文件的读、写或追加。iomode参数指定了要求的访问类型,允许值是ForReading(1) (缺省值)、ForWrite(2)、ForAppending(8)。format参数说明了读、写文件的数据格式。允许值是TristateFalse(0)(缺省),说明用ASCII数据格式;TristateTrue(-1)说明用Unicode数据格式;TristateUseDefault(-2)说明使用系统缺省格式</p> <p> 因此给定一个File对象后,可以使用ParentFolder属性得到包含该文件的Folder对象的引用,用来在文件系统中导航。甚至可以用Drive属性获得相应的Drive对象的引用,并得到各种Folder对象以及所包含的File对象。<br /> 另外,给定一个Folder对象以及对应的Files集合后,可以通过遍历该集合检查这一文件夹中的每个文件。还可以使用File对象的各种方法以一定方式处理该文件,如复制、移动或删除。下面的代码给出了C驱动器的第一个文件夹的文件列表:<br /> ' In VBScript:<br /> ' Create a FileSystemObject instance<br /> Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)<br /> ' Get a reference to drive C<br /> Set objDriveC = objFSO.GetDrive(&quot;C:&quot;)<br /> ' Get a reference to the root folder<br /> Set objRoot = objDriveC.RootFolder<br /> ' Get a reference to the SubFolders collection<br /> Set objFolders = objRoot.SubFolders<br /> ' Get a reference to the first folder in the SubFolders collection<br /> For Each objFolder In objFolders<br /> Set objFolder1 = objFolders.Item((objFolder.Name))<br /> Exit For<br /> Next<br /> ' Iterate through all the files in this folder<br /> For Each objFile in objFolder1.Files<br /> Response.Write &quot;Name: &quot; &amp; objFile.Name &amp; &quot; &quot;<br /> Response.Write &quot;ShortName: &quot; &amp; objFile.ShortName &amp; &quot; &quot;<br /> Response.Write &quot;Size: &quot; &amp; objFile.Size &amp; &quot; bytes &quot;<br /> Response.Write &quot;Type: &quot; &amp; objFile.Type &amp; &quot;&lt;BR&gt;&quot;<br /> Response.Write &quot;Path: &quot; &amp; objFile.Path &amp; &quot; &quot;<br /> Response.Write &quot;ShortPath: &quot; &amp; objFile.ShortPath &amp; &quot;&lt;BR&gt;&quot;<br /> Response.Write &quot;Created: &quot; &amp; objFile.DateCreated &amp; &quot; &quot;<br /> Response.Write &quot;LastModified: &quot; &amp; objFile.DateLastModified &amp; &quot;&lt;P&gt;&quot;<br /> Next<br /> 注意,不能使用数字索引来定位Folders或Files集合里的条目,因此必须使用For Each &hellip; Next语句遍历该集合直到最初的条目,然后使用该条目的Name属性。也不得不使用嵌套的圆括号强迫其作为值(字符串)传送给该Folders集合的Item方法。<br /> 用下面的JScript程序可完成同样的工作:<br /> // In JScript:<br /> // Create a FileSystemObject instance<br /> var objFSO = Server.CreateObject('Scripting.FileSystemObject');<br /> // Get a reference to drive C<br /> var objDriveC = objFSO.GetDrive('C:');<br /> // Get a reference to the root folder<br /> var objRoot = objDriveC.RootFolder;<br /> // Get a reference to the first folder in the SubFolders collection<br /> var colAllFolders = new Enumerator(objRoot.SubFolders);<br /> var objFolder1 = colAllFolders.item();<br /> // Get a reference to the Files collection for this folder<br /> var colFiles = new Enumerator(objFolder1.Files);</p> <p>// Iterate through all the files in this collection<br /> for (; !colFiles.atEnd(); colFiles.moveNext()) {<br /> objFile = colFiles.item()<br /> Response.Write('Name: ' + objFile.Name + ' ');<br /> Response.Write('ShortName: ' + objFile.ShortName + ' ');<br /> Response.Write('Size: ' + objFile.Size + ' bytes ');<br /> Response.Write('Type: ' + objFile.Type + '&lt;BR&gt;');<br /> Response.Write('Path: ' + objFile.Path + ' ');<br /> Response.Write('ShortPath: ' + objFile.ShortPath + '&lt;BR&gt;');<br /> Response.Write('Created: ' + objFile.DateCreated + ' ');<br /> Response.Write('Accessed: ' + objFile.DateLastAccessed + ' ');<br /> Response.Write('Modified: ' + objFile.DateLastModified + '&lt;P&gt;');<br /> }<br /> 两个程序的结果是相同的,如图5-14所示。该页面为filescollection_vb.asp,来自本书提供的示例文件。</p> <p><br /> 在C驱动器上的第一个文件夹可能是CAConfig,缺省情况下该文件夹是空的。这种情况下,可先向该文件夹复制一些文件,完成实验以后再删除这些文件。</p>
返回顶部 留言