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所示:
因此,通过使用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);
在文件夹之间可以使用当前文件夹的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);
因此给定一个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);