FileSystemObject(FSO) 示例程式碼 (轉)

worldblog發表於2007-12-12
FileSystemObject(FSO) 示例程式碼 (轉)[@more@]

FileSystem
示例程式碼

在本節描述的示例程式碼,提供真實的例子來示範在 FileSystemObject 中可用的許多功能。該程式碼顯示瞭如何一起使用物件模式的所有功能,以及如何在您自己的程式碼中有效地使用這些功能。

請注意,由於該程式碼是極一般的,所以要使該程式碼能夠真正在您的機器上執行,可能需要一些其他程式碼和小小的變更。這些改變之所以必要,是因為在 Active Server Pages 和 Scripting Host 之間,為輸入和輸出給採用了不同的方法。

要在 Active Server Pages 上執行該程式碼,則採取以下步驟:

  1. 建立一個標準的 頁,字尾名為 .。
  2. 把下面的示例程式碼複製到 ... 標記之間的中。
  3. 把所有程式碼包裝器到 標記內。
  4. Option Explicit 語句從當前位置移動到 HTML 頁的最頂部,甚至在 開始標記前。
  5. 把 標記放置在 Option Explicit 語句周圍,以保證它在端執行。
  6. 把下面的程式碼新增到示例程式碼末尾:

Sub Print(x) Response.Write "

" Response.Write x Response.Write "
" End Sub Main
前面的程式碼增加一個將在伺服器端執行,但在客戶端顯示結果的列印過程。要在 Windows Scripting Host 上執行該程式碼,則把下面的程式碼新增到示例程式碼的末尾:

Sub Print(x) WScript.Echo x End Sub Main

下面就是示例程式碼:


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' FileSystemObject 示例程式碼 ' 'Copyright 1998 Corporation。保留所有權利。 ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' 對於程式碼質量: ' ' 1) 下面的程式碼有許多字串操作,用"&"運算子來把短字串連線在一起。由於 ' 字串連線是費時的,所以這是一種低的寫程式碼方法。無論如何,它是 ' 一種非常好維護的寫程式碼方法,並且在這兒使用了這種方法,因為該 ' 大量的操作,而磁碟操作比連線字串所需的操作要慢得多。 ' 記住這是示範程式碼,而不是產品程式碼。 ' ' 2) 使用了 "Option Explicit",因為訪問宣告過的變數,比訪問未宣告的變數要 ' 稍微快一些。它還能阻止在程式碼中發生錯誤,例如,把 DriveTypeCDROM 誤拼 ' 成了 DriveTypeCDO。 ' ' 3) 為了使程式碼更可讀,該程式碼中沒有錯誤處理。雖然採取了防範措施,來保證程式碼 ' 在普通情況下沒有錯誤,但檔案是不可預知的。在產品程式碼中,使用 ' On Error Resume Next 和 Err 物件來捕獲可能發生的錯誤。 ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' 一些容易取得的全域性變數 ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim TabStop Dim NewLine Const TestDrive = "C" Const TestFilePath = "C:Test" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' 由 Drive.DriveType 返回的常數 ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Const DriveTypeRemovable = 1 Const DriveTypeFixed = 2 Const DriveTypwork = 3 Const DriveTypeCDROM = 4 Const DriveTypeRisk = 5 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' 由 File.Attributes 返回的常數 ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Const FileAttrNormal = 0 Const FileAttrReadOnly = 1 Const FileAttrHidden = 2 Const FileAttrSystem = 4 Const FileAttrVolume = 8 Const FileAttrDirectory = 16 Const FileAttrArchive = 32 Const FileAttrAlias = 64 Const FileAttrCompressed = 128 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' 用來開啟檔案的常數 ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Const OpenFileForReading = 1 Const OpenFileForWriting = 2 Const OpenFileForAppending = 8 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' ShowDriveType ' ' 目的: ' ' 生成一個字串,來描述給定 Drive 物件的器型別。 ' ' 示範下面的內容 ' ' - Drive.DriveType ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function ShowDriveType(Drive) Dim S Case Drive.DriveType Case DriveTypeRemovable S = "Removable" Case DriveTypeFixed S = "Fixed" Case DriveTypeNetwork S =work" Case DriveTypeCDROM S = "CD-ROM" Case DriveTypeRAMDisk S = "RAM Disk" Case Else S = "Unknown" End Select ShowDriveType = S End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' ShowFileAttr ' ' 目的: ' ' 生成一個字串,來描述檔案或資料夾的屬性。 ' ' 示範下面的內容 ' ' - File.Attributes ' - Folder.Attributes ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function ShowFileAttr(File) ' File 可以是檔案或資料夾 Dim S Dim Attr Attr = File.Attributes If Attr = 0 Then ShowFileAttr = "Normal" Exit Function End If If Attr And FileAttrDirectory Then S = S & "Directory " If Attr And FileAttrReadOnly Then S = S & "Read-Only " If Attr And FileAttrHidden Then S = S & "Hidden " If Attr And FileAttrSystem Then S = S & "System " If Attr And FileAttrVolume Then S = S & "Volume " If Attr And FileAttrArchive Then S = S & "Archive " If Attr And FileAttrAlias Then S = S & "Alias " If Attr And FileAttrCompressed Then S = S & "Compressed " ShowFileAttr = S End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' GenerateDriveInformation ' ' 目的: ' ' 生成一個字串,來描述可用驅動器的當前狀態。 ' ' 示範下面的內容 ' ' - FileSystemObject.Drives ' - Iterating the Drives collection ' - Drives.Count ' - Drive.AvailableSpace ' - Drive.DriveLetter ' - Drive.DriveType ' - Drive.FileSystem ' - Drive.FreeSpace ' - Drive.IsReady ' - Drive.Path ' - Drive.SerialNumber ' - Drive.ShareName ' - Drive.TotalSize ' - Drive.VolumeName ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ) Dim Drives Dim Drive Dim S Set Drives = FSO.Drives S = "Number of drives:" & TabStop & Drives.Count & NewLine & NewLine ' 構造報告的第一行。 S = S & String(2, TabStop) & "Drive" S = S & String(3, TabStop) & "File" S = S & TabStop & "Total" S = S & TabStop & "Free" S = S & TabStop & "Available" S = S & TabStop & "Serial" & NewLine ' 構造報告的第二行。 S = S & "Letter" S = S & TabStop & "Path" S = S & TabStop & "Type" S = S & TabStop & "Ready?" S = S & TabStop & "Name" S = S & TabStop & "System" S = S & TabStop & "Space" S = S & TabStop & "Space" S = S & TabStop & "Space" S = S & TabStop & "Number" & NewLine ' 分隔行。 S = S & String(105, "-") & NewLine For Each Drive In Drives S = S & Drive.DriveLetter S = S & TabStop & Drive.Path S = S & TabStop & ShowDriveType(Drive) S = S & TabStop & Drive.IsReady If Drive.IsReady Then If DriveTypeNetwork = Drive.DriveType Then S = S & TabStop & Drive.ShareName Else S = S & TabStop & Drive.VolumeName End If S = S & TabStop & Drive.FileSystem S = S & TabStop & Drive.TotalSize S = S & TabStop & Drive.FreeSpace S = S & TabStop & Drive.AvailableSpace S = S & TabStop & Hex(Drive.SerialNumber) End If S = S & NewLine Next GenerateDriveInformation = S End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' GenerateFileInformation ' ' 目的: ' ' 生成一個字串,來描述檔案的當前狀態。 ' ' 示範下面的內容 ' ' - File.Path ' - File.Name ' - File.Type ' - File.DateCreated ' - File.DateLastAccessed ' - File.DateLastModified ' - File.Size ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function GenerateFileInformation(File) Dim S S = NewLine & "Path:" & TabStop & File.Path S = S & NewLine & "Name:" & TabStop & File.Name S = S & NewLine & "Type:" & TabStop & File.Type S = S & NewLine & "Attribs:" & TabStop & ShowFileAttr(File) S = S & NewLine & "Created:" & TabStop & File.DateCreated S = S & NewLine & "Accessed:" & TabStop & File.DateLastAccessed S = S & NewLine & "Modified:" & TabStop & File.DateLastModified S = S & NewLine & "Size" & TabStop & File.Size & NewLine GenerateFileInformation = S End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' GenerateFolderInformation ' ' 目的: ' ' 生成一個字串,來描述資料夾的當前狀態。 ' ' 示範下面的內容 ' ' - Folder.Path ' - Folder.Name ' - Folder.DateCreated ' - Folder.DateLastAccessed ' - Folder.DateLastModified ' - Folder.Size ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function GenerateFolderInformation(Folder) Dim S S = "Path:" & TabStop & Folder.Path S = S & NewLine & "Name:" & TabStop & Folder.Name S = S & NewLine & "Attribs:" & TabStop & ShowFileAttr(Folder) S = S & NewLine & "Created:" & TabStop & Folder.DateCreated S = S & NewLine & "Accessed:" & TabStop & Folder.DateLastAccessed S = S & NewLine & "Modified:" & TabStop & Folder.DateLastModified S = S & NewLine & "Size:" & TabStop & Folder.Size & NewLine GenerateFolderInformation = S End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' GenerateAllFolderInformation ' ' 目的: ' ' 生成一個字串,來描述一個資料夾和所有檔案及子資料夾的當前狀態。 ' ' 示範下面的內容 ' ' - Folder.Path ' - Folder.SubFolders ' - Folders.Count ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function GenerateAllFolderInformation(Folder) Dim S Dim SubFolders Dim SubFolder Dim Files Dim File S = "Folder:" & TabStop & Folder.Path & NewLine & NewLine Set Files = Folder.Files If 1 = Files.Count Then S = S & "There is 1 file" & NewLine Else S = S & "There are " & Files.Count & " files" & NewLine End If If Files.Count <> 0 Then For Each File In Files S = S & GenerateFileInformation(File) Next End If Set SubFolders = Folder.SubFolders If 1 = SubFolders.Count Then S = S & NewLine & "There is 1 sub folder" & NewLine & NewLine Else S = S & NewLine & "There are " & SubFolders.Count & " sub folders" & NewLine & NewLine End If If SubFolders.Count <> 0 Then For Each SubFolder In SubFolders S = S & GenerateFolderInformation(SubFolder) Next S = S & NewLine For Each SubFolder In SubFolders S = S & GenerateAllFolderInformation(SubFolder) Next End If GenerateAllFolderInformation = S End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' GenerateTestInformation ' ' 目的: ' ' 生成一個字串,來描述 C:Test 資料夾和所有檔案及子資料夾的當前狀態。 ' ' 示範下面的內容 ' ' - FileSystemObject.DriveExists ' - FileSystemObject.FolderExists ' - FileSystemObject.GetFolder ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function GenerateTestInformation(FSO) Dim TestFolder Dim S If Not FSO.DriveExists(TestDrive) Then Exit Function If Not FSO.FolderExists(TestFilePath) Then Exit Function Set TestFolder = FSO.GetFolder(TestFilePath) GenerateTestInformation = GenerateAllFolderInformation(TestFolder) End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' DeleteTestDirectory ' ' 目的: ' ' 清理 test 目錄。 ' ' 示範下面的內容 ' ' - FileSystemObject.GetFolder ' - FileSystemObject.DeleteFile ' - FileSystemObject.DeleteFolder ' - Folder.Delete ' - File.Delete ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Sub DeleteTestDirectory(FSO) Dim TestFolder Dim SubFolder Dim File TextStream.Close GetLyrics = S End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' BuildTestDirectory ' ' 目的: ' ' 建立一個目錄分層結構來示範 FileSystemObject。 ' ' 以這樣的次序來建立分層結構: ' ' C:Test ' C:TestReadMe.txt ' C:TestBeatles ' C:TestBeatlesOctopusGarden.txt ' C:TestBeatlesBathroomWindow.txt ' ' ' 示範下面的內容 ' ' - FileSystemObject.DriveExists ' - FileSystemObject.FolderExists ' - FileSystemObject.CreateFolder ' - FileSystemObject.CreateTextFile ' - Folders.Add ' - Folder.CreateTextFile ' - TextStream.WriteLine ' - TextStream.Close ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


 


相信這個例子已經含蓋了大部分的FSO的應用,大家可以根據這個例子來修改,得到自己


想要的功能,期待和大家交流:)


cpplus


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-992192/,如需轉載,請註明出處,否則將追究法律責任。

相關文章