http://support.microsoft.com/kb/190029/zh-tw
結論
本文告訴您如何列印出檔案屬性,例如建立日期]、 [短檔名和 [所有檔案的大小在指定
的資料夾使用 FileSystemObject。 本文介紹建立專案,會列印出至指定的文字檔的檔案
資訊的方法。
回此頁最上方
其他相關資訊
本文會假設您已熟悉在 Visual Basic 中使用物件、 集合和物件模型。
FileSystemObject 可讓您在檔案系統的電腦上,執行一些工作。 藉由在 Microsoft 指
令碼的執行階段檔案 Scrrun.dll 的參考,這個物件可供您的專案。 這個檔案隨附於
Windows 98 和下列產品:
Windows Script Host
Windows NT Option Pack
Microsoft Internet Information Server 3.0
指令碼 [3.1 升級
Visual Studio 98
Visual Basic 6.0
指令碼執行階段檔案參考之後中,,您建立使用 CreateObject 方法,FileSystemObject
的執行個體。 FileSystemObject 可以使用 CreateTextFile 方法,以建立文字檔案。
您寫入至文字檔使用寫入,在的 WriteBlankLines 或 WriteLine 函式。
若要取得指定的資料夾中所有檔案的屬性,開啟資料夾使用 GetFolder 方法]。 您可以
再執行迴圈的檔案集合的每個成員,並取得每個檔案的屬性。
下一個區段會顯示您如何建立範例專案,示範使用 FileSystemObject,取得指定的目錄
中所有檔案的屬性。 然後,這些屬性會列印至文字檔。
回此頁最上方
建立範例專案的步驟
如果要在 Visual Basic 中開啟一個新的標準 EXE 專案。 根據預設會建立 Form1。
將參考加入至 Microsoft 的指令碼執行階段藉由完成下列步驟:
從 [ 專案 ] 功能表中,按一下 [ 參考 ],以顯示 [ 參考 ] 對話方塊。
按一下 [瀏覽] 以開啟 [ 加入參考 ] 對話方塊。
選取檔案 scrrun.dll 並按 (一下) [確定] ],以關閉 [ 加入參考 ] 對話方塊。 這個
檔案被安裝在您的系統目錄中。 Microsoft Scripting Runtime 會出現在 [ 參考 ] 對
話方塊的 [ 可用的參考 ] 清單方塊檢查。
按一下 [ 確定 ],以關閉 [ 參考 ] 對話方塊]。
將下列控制項加入至 Form1:
命令按鈕
DriveListBox
DirListBox
標籤
將下列程式碼複製到 Form1 的程式碼視窗:
Option Explicit
Private Sub Form_Load()
Label1.AutoSize = True
Label1.Caption = Dir1.Path
Dir1.Path = Drive1.Drive
Command1.Caption = "Create File List"
End Sub
Private Sub Dir1_Change()
Label1.Caption = Dir1.Path
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub Command1_Click()
Dim fso As FileSystemObject
Dim f As Folder
Dim fc As Files
Dim fl As File
Dim ts As TextStream
Dim sFileName As String
Dim sVersion As String
Set fso = CreateObject("Scripting.FileSystemObject")
sFileName = InputBox("Enter File Name", _
"Enter Path and File Name")
If sFileName = "" Then
Exit Sub
Else
Set ts = fso.CreateTextFile(sFileName, True, False)
Set f = fso.GetFolder(Dir1.Path)
Set fc = f.Files
With ts
.WriteLine ("Files in " & Dir1.Path)
.WriteBlankLines (1)
For Each fl In fc
.WriteLine ("Filename: " & fl.Name)
.WriteLine (vbTab & "File Type: " & fl.Type)
' Get Version Information
sVersion = fso.GetFileVersion(fl.Path)
If sVersion <> "" Then
.WriteLine (vbTab & "Version: " & fso.GetFileVersion(fl.Path))
Else
.WriteLine (vbTab & "No Version Information")
End If
.WriteLine (vbTab & "Created: " & fl.DateCreated)
.WriteLine (vbTab & "Last Modified: " & _
fl.DateLastModified)
.WriteLine (vbTab & "Last Accessed: " & _
fl.DateLastAccessed)
.WriteLine (vbTab & "Path: " & fl.Path)
.WriteLine (vbTab & "Short Name: " & fl.ShortName)
.WriteLine (vbTab & "Size: " & fl.Size & " bytes")
.WriteBlankLines (1)
Next
End With
MsgBox (sFileName & " created.")
End If
'Destroy All Objects
Set fc = Nothing
Set f = Nothing
Set ts = Nothing
Set fso = Nothing
End Sub
[執行] 功能表上按一下 [開始] ,或按 F5 鍵以啟動程式]。
選取磁碟機及資料夾。 在路徑會出現在 標籤控制項 中。
按一下 [ 建立檔案清單 ] 命令按鈕。 執行輸入的對話方塊。
輸入文字檔會包含檔案內容的路徑和檔案名稱。 按一下 [ 確定 ],以關閉 [輸入] 方塊
]。
建立文字檔時,訊息方塊隨即出現。 開啟文字檔來檢視結果。