在文件夾之間可以使用當前文件夾的ParentFolder屬性,返回到父目錄。當到達一個文件夾時,如果IsRootFolder屬性是True,就停下來。離開驅動器的根目錄,沿目錄樹向下,可遍歷或訪問在Folders集合(由當前文件夾的SubFolders屬性返回)內的指定文件夾。 下列程序遍歷了驅動器C根目錄內的所有文件夾,并顯示各個文件夾的有關信息。 VBScr' 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);