| JScript? | [語言參考](#) |
|-----|-----|
# FileSystemObject 示例代碼
在本節描述的示例代碼,提供真實的例子來示范在 **FileSystemObject** 對象模式中可用的許多功能。該代碼顯示了如何一起使用對象模式的所有功能,以及如何在您自己的代碼中有效地使用這些功能。
請注意,由于該代碼是極一般的,所以要使該代碼能夠真正在您的機器上運行,可能需要一些其他代碼和小小的變更。這些改變之所以必要,是因為在 Active Server Pages 和 Windows Scripting Host 之間,為輸入和輸出給用戶采用了不同的方法。
要在 Active Server Pages 上運行該代碼,則采取以下步驟:
創建一個標準的 Web 頁,后綴名為 .asp。
把下面的示例代碼復制到 <BODY>...</BODY> 標記之間的文件中。
把所有代碼包裝器到 <%...%> 標記內。
把 **Option Explicit** 語句從當前位置移動到 HTML 頁的最頂部,甚至在 <HTML> 開始標記前。
把 <%...%> 標記放置在 **Option Explicit** 語句周圍,以保證它在服務器端運行。
把下面的代碼添加到示例代碼末尾:
~~~
Sub Print(x)
~~~
???
~~~
Response.Write "<PRE><FONT FACE=""
~~~
宋體
~~~
"" SIZE=""1"">"
~~~
~~~
???Response.Write x
~~~
~~~
???Response.Write "</FONT></PRE>"
~~~
~~~
End Sub
~~~
~~~
Main
~~~
前面的代碼增加一個將在服務器端運行,但在客戶端顯示結果的打印過程。要在 Windows Scripting Host 上運行該代碼,則把下面的代碼添加到示例代碼的末尾:
~~~
Sub Print(x)
~~~
???
~~~
WScript.Echo x
~~~
~~~
End Sub
~~~
~~~
Main
~~~
下面就是示例代碼:
~~~
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' FileSystemObject
~~~
示例代碼
~~~
'Copyright 1998 Microsoft Corporation
~~~
。???保留所有權利。
~~~
~~~
~~~
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
~~~
~~~
Option Explicit
~~~
~~~
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
~~~
~~~
'
~~~
對于代碼質量:
~~~
' 1)
~~~
下面的代碼有許多字符串操作,用
~~~
"&"
~~~
運算符來把短字符串連接在一起。由于
~~~
'
~~~
字符串連接是費時的,所以這是一種低效率的寫代碼方法。無論如何,它是
~~~
'
~~~
一種非常好維護的寫代碼方法,并且在這兒使用了這種方法,因為該程序執行
~~~
'
~~~
大量的磁盤操作,而磁盤操作比連接字符串所需的內存操作要慢得多。
~~~
'
~~~
記住這是示范代碼,而不是產品代碼。
~~~
'
~~~
~~~
' 2)
~~~
使用了
~~~
"Option Explicit"
~~~
,因為訪問聲明過的變量,比訪問未聲明的變量要
~~~
'
~~~
稍微快一些。它還能阻止在代碼中發生錯誤,例如,把
~~~
DriveTypeCDROM
~~~
誤拼
~~~
'
~~~
成了
~~~
DriveTypeCDORM
~~~
。
~~~
'
~~~
~~~
' 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 DriveTypeNetwork = 3
~~~
~~~
Const DriveTypeCDROM = 4
~~~
~~~
Const DriveTypeRAMDisk = 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
~~~
???
???
~~~
Select Case Drive.DriveType
~~~
???
~~~
Case DriveTypeRemovable
~~~
??????
~~~
S = "Removable"
~~~
???
~~~
Case DriveTypeFixed
~~~
??????
~~~
S = "Fixed"
~~~
???
~~~
Case DriveTypeNetwork
~~~
??????
~~~
S = "Network"
~~~
???
~~~
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
~~~
~~~
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
~~~
~~~
Function GenerateDriveInformation(FSO)
~~~
???
~~~
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
~~~
~~~
???
???
~~~
'
~~~
有兩種方法可用來刪除文件:
???
~~~
FSO.DeleteFile(TestFilePath & "\Beatles\OctopusGarden.txt")
~~~
???
~~~
Set File = FSO.GetFile(TestFilePath & "\Beatles\BathroomWindow.txt")
~~~
???
~~~
File.Delete
~~~
???
???
~~~
'
~~~
有兩種方法可用來刪除文件夾:
???
~~~
FSO.DeleteFolder(TestFilePath & "\Beatles")
~~~
???
~~~
FSO.DeleteFile(TestFilePath & "\ReadMe.txt")
~~~
???
~~~
Set TestFolder = FSO.GetFolder(TestFilePath)
~~~
???
~~~
TestFolder.Delete
~~~
~~~
End Sub
~~~
~~~
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
~~~
~~~
' CreateLyrics
~~~
~~~
'
~~~
目的:
~~~
~~~
~~~
'
~~~
在文件夾中創建兩個文本文件。
~~~
'
~~~
示范下面的內容
~~~
' - FileSystemObject.CreateTextFile
~~~
~~~
' - TextStream.WriteLine
~~~
~~~
' - TextStream.Write
~~~
~~~
' - TextStream.WriteBlankLines
~~~
~~~
' - TextStream.Close
~~~
~~~
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
~~~
~~~
Sub CreateLyrics(Folder)
~~~
???
~~~
Dim TextStream
~~~
~~~
~~~
???
???
~~~
Set TextStream = Folder.CreateTextFile("OctopusGarden.txt")
~~~
~~~
~~~
???
???
~~~
TextStream.Write("Octopus' Garden ") '
~~~
請注意,該語句不添加換行到文件中。
???
~~~
TextStream.WriteLine("(by Ringo Starr)")
~~~
???
~~~
TextStream.WriteBlankLines(1)
~~~
???
~~~
TextStream.WriteLine("I'd like to be under the sea in an octopus' garden in the shade,")
~~~
???
~~~
TextStream.WriteLine("He'd let us in, knows where we've been -- in his octopus' garden in the shade.")
~~~
???
~~~
TextStream.WriteBlankLines(2)
~~~
???
???
~~~
TextStream.Close
~~~
???
~~~
Set TextStream = Folder.CreateTextFile("BathroomWindow.txt")
~~~
???
~~~
TextStream.WriteLine("She Came In Through The Bathroom Window (by Lennon/McCartney)")
~~~
???
~~~
TextStream.WriteLine("")
~~~
???
~~~
TextStream.WriteLine("She came in through the bathroom window protected by a silver spoon")
~~~
???
~~~
TextStream.WriteLine("But now she sucks her thumb and wanders by the banks of her own lagoon")
~~~
???
~~~
TextStream.WriteBlankLines(2)
~~~
???
~~~
TextStream.Close
~~~
~~~
End Sub
~~~
~~~
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
~~~
~~~
' GetLyrics
~~~
~~~
'
~~~
目的:
~~~
~~~
~~~
'
~~~
顯示
~~~
lyrics
~~~
文件的內容。
~~~
'
~~~
示范下面的內容
~~~
' - FileSystemObject.OpenTextFile
~~~
~~~
' - FileSystemObject.GetFile
~~~
~~~
' - TextStream.ReadAll
~~~
~~~
' - TextStream.Close
~~~
~~~
' - File.OpenAsTextStream
~~~
~~~
' - TextStream.AtEndOfStream
~~~
~~~
' - TextStream.ReadLine
~~~
~~~
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
~~~
~~~
Function GetLyrics(FSO)
~~~
???
~~~
Dim TextStream
~~~
???
~~~
Dim S
~~~
???
~~~
Dim File
~~~
???
~~~
'
~~~
有多種方法可用來打開一個文本文件,和多種方法來從文件讀取數據。
???
~~~
'
~~~
這兒用了兩種方法來打開文件和讀取文件:
???
~~~
Set TextStream = FSO.OpenTextFile(TestFilePath & "\Beatles\OctopusGarden.txt", OpenFileForReading)
~~~
~~~
~~~
???
???
~~~
S = TextStream.ReadAll & NewLine & NewLine
~~~
???
~~~
TextStream.Close
~~~
???
~~~
Set File = FSO.GetFile(TestFilePath & "\Beatles\BathroomWindow.txt")
~~~
???
~~~
Set TextStream = File.OpenAsTextStream(OpenFileForReading)
~~~
???
~~~
Do
~~~
???
~~~
While Not TextStream.AtEndOfStream
~~~
??????
~~~
S = S & TextStream.ReadLine & NewLine
~~~
???
~~~
Loop
~~~
???
~~~
TextStream.Close
~~~
???
~~~
GetLyrics = S
~~~
???
~~~
End Function
~~~
~~~
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
~~~
~~~
' BuildTestDirectory
~~~
~~~
'
~~~
目的:
~~~
~~~
~~~
'
~~~
創建一個目錄分層結構來示范
~~~
FileSystemObject
~~~
。
~~~
'
~~~
以這樣的次序來創建分層結構:
~~~
' C:\Test
~~~
~~~
' C:\Test\ReadMe.txt
~~~
~~~
' C:\Test\Beatles
~~~
~~~
' C:\Test\Beatles\OctopusGarden.txt
~~~
~~~
' C:\Test\Beatles\BathroomWindow.txt
~~~
~~~
'
~~~
示范下面的內容
~~~
' - FileSystemObject.DriveExists
~~~
~~~
' - FileSystemObject.FolderExists
~~~
~~~
' - FileSystemObject.CreateFolder
~~~
~~~
' - FileSystemObject.CreateTextFile
~~~
~~~
' - Folders.Add
~~~
~~~
' - Folder.CreateTextFile
~~~
~~~
' - TextStream.WriteLine
~~~
~~~
' - TextStream.Close
~~~
~~~
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
~~~
~~~
Function BuildTestDirectory(FSO)
~~~
???
~~~
Dim TestFolder
~~~
???
~~~
Dim SubFolders
~~~
???
~~~
Dim SubFolder
~~~
???
~~~
Dim TextStream
~~~
???
~~~
'
~~~
排除
~~~
(a)
~~~
驅動器不存在,或
~~~
(b)
~~~
要創建的目錄已經存在的情況。
???
~~~
If Not FSO.DriveExists(TestDrive) Then
~~~
??????
~~~
BuildTestDirectory = False
~~~
??????
~~~
Exit Function
~~~
???
~~~
End If
~~~
???
~~~
If FSO.FolderExists(TestFilePath) Then
~~~
??????
~~~
BuildTestDirectory = False
~~~
??????
~~~
Exit Function
~~~
???
~~~
End If
~~~
???
~~~
Set TestFolder = FSO.CreateFolder(TestFilePath)
~~~
???
~~~
Set TextStream = FSO.CreateTextFile(TestFilePath & "\ReadMe.txt")
~~~
???
~~~
TextStream.WriteLine("My song lyrics collection")
~~~
???
~~~
TextStream.Close
~~~
???
~~~
Set SubFolders = TestFolder.SubFolders
~~~
???
~~~
Set SubFolder = SubFolders.Add("Beatles")
~~~
???
~~~
CreateLyrics SubFolder
~~~
???
???
~~~
BuildTestDirectory = True
~~~
~~~
End Function
~~~
~~~
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
~~~
~~~
'
~~~
主程序
~~~
'
~~~
首先,它創建一個
~~~
test
~~~
目錄,以及一些子文件夾和文件。
~~~
~~~
~~~
'
~~~
然后,它轉儲有關可用磁盤驅動器和
~~~
test
~~~
目錄的某些信息,
~~~
'
~~~
最后,清除
~~~
test
~~~
目錄及其所有內容。
~~~
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
~~~
~~~
Sub Main
~~~
???
~~~
Dim FSO
~~~
???
~~~
'
~~~
設立全局變量。
???
~~~
TabStop = Chr(9)
~~~
???
~~~
NewLine = Chr(10)
~~~
~~~
~~~
???
???
~~~
Set FSO = CreateObject("Scripting.FileSystemObject")
~~~
???
~~~
If Not BuildTestDirectory(FSO) Then
~~~
??????
~~~
Print "Test directory already exists or cannot be created. Cannot continue."
~~~
??????
~~~
Exit Sub
~~~
???
~~~
End If
~~~
???
~~~
Print GenerateDriveInformation(FSO) & NewLine & NewLine
~~~
???
~~~
Print GenerateTestInformation(FSO) & NewLine & NewLine
~~~
???
~~~
Print GetLyrics(FSO) & NewLine & NewLine
~~~
???
~~~
DeleteTestDirectory(FSO)
~~~
~~~
End Sub
~~~
~~~
[? 2000 Microsoft Corporation 版權所有。保留所有權利。使用規定。](#)
- JScript
- JScript 用戶指南
- Jscript 基礎
- 什么是 JScript?
- 編寫 JScript 代碼
- JScript 的變量
- JScript 的數據類型
- JScript 的運算符
- 控制程序的流程
- JScript 函數
- Jscript對象
- 創建自己的對象
- 內部對象
- JScript 保留關鍵字
- 高級 JScript
- 創建高級對象
- 遞歸
- 變量范圍
- 復制、傳遞和比較數據
- 使用數組
- 特殊字符
- 腳本問題解答
- 條件編譯
- 條件編譯變量
- 在瀏覽器中顯示信息
- 使用消息框
- 語言參考
- 特性信息
- Microsoft JScript 特性 - ECMA
- Microsoft JScript 特性 - 非-ECMA
- JScript 字母順序的關健字列表
- JScript 錯誤
- JScript 運行時錯誤
- JScript 語法錯誤
- JScript 函數
- GetObject 函數
- ScriptEngine 函數
- ScriptEngineBuildVersion 函數
- ScriptEngineMajorVersion 函數
- ScriptEngineMinorVersion 函數
- JScript 方法
- A-E
- abs 方法
- acos 方法
- anchor 方法
- apply 方法
- asin 方法
- atan 方法
- atan2 方法
- atEnd 方法
- big 方法
- blink 方法
- bold 方法
- call 方法
- ceil 方法
- charAt 方法
- charCodeAt 方法
- compile 方法
- concat 方法 (Array)
- concat 方法 (String)
- cos 方法
- decodeURI 方法
- decodeURIComponent 方法
- dimensions 方法
- encodeURI 方法
- encodeURIComponent 方法
- escape 方法
- eval 方法
- exec 方法
- exp 方法
- F-I
- fixed 方法
- floor 方法
- fontcolor 方法
- fontsize 方法
- fromCharCode 方法
- getDate 方法
- getDay 方法
- getFullYear 方法
- getHours 方法
- getItem 方法
- getMilliseconds 方法
- getMinutes 方法
- getMonth 方法
- getSeconds 方法
- getTime 方法
- getTimezoneOffset 方法
- getUTCDate 方法
- getUTCDay 方法
- getUTCFullYear 方法
- getUTCHours 方法
- getUTCMilliseconds 方法
- getUTCMinutes 方法
- getUTCMonth 方法
- getUTCSeconds 方法
- getVarDate 方法
- getYear 方法
- indexOf 方法
- isFinite 方法
- isNaN 方法
- italics 方法
- item 方法
- J-R
- join 方法
- lastIndexOf 方法
- lbound 方法
- link 方法
- localeCompare 方法
- log 方法
- match 方法
- max 方法
- min 方法
- moveFirst 方法
- moveNext 方法
- parse 方法
- parseFloat 方法
- parseInt 方法
- pop 方法
- pow 方法
- push 方法
- random 方法
- replace 方法
- reverse 方法
- round 方法
- S
- search 方法
- setDate 方法
- setFullYear 方法
- setHours 方法
- setMilliseconds 方法
- setMinutes 方法
- setMonth 方法
- setSeconds 方法
- setTime 方法
- setUTCDate 方法
- setUTCFullYear 方法
- setUTCHours 方法
- setUTCMilliseconds 方法
- setUTCMinutes 方法
- setUTCMonth 方法
- setUTCSeconds 方法
- setYear 方法
- shift 方法
- sin 方法
- slice 方法 (Array)
- slice 方法 (String)
- small 方法
- sort 方法
- splice 方法
- split 方法
- sqrt 方法
- strike 方法
- sub 方法
- substr 方法
- substring 方法
- sup 方法
- T-Z
- tan 方法
- test 方法
- toArray 方法
- toDateString 方法
- toExponential 方法
- toFixed 方法
- toGMTString 方法
- toLocaleDateString 方法
- toLocaleLowerCase 方法
- toLocaleString 方法
- toLocaleTimeString 方法
- toLocaleUpperCase 方法
- toLowerCase 方法
- toPrecision 方法
- toString 方法
- toTimeString 方法
- toUpperCase 方法
- toUTCString 方法
- ubound 方法
- unescape 方法
- unshift 方法
- UTC 方法
- valueOf 方法
- JScript 對象
- ActiveXObject 對象
- Array 對象
- Boolean 對象
- Date 對象
- Enumerator 對象
- Error 對象
- Function 對象
- Global 對象
- Math 對象
- Number 對象
- Object 對象
- RegExp 對象
- 正則表達式對象
- String 對象
- VBArray 對象
- JScript 運算符
- 通用信息
- 運算符優先級
- 運算符總結
- 加法賦值運算符 (+=)
- 加法運算符 (+)
- 賦值運算符 (=)
- 按位“與”賦值運算符 (&=)
- 按位“與”運算符 (&)
- 按位左移運算符 (<<)
- 按位“非”運算符 (~)
- 按位“或”賦值運算符 (|=)
- 按位“或”運算符 (|)
- 按位右移運算符 (>>)
- 按位“異或”賦值運算符 (^=)
- 按位“異或”運算符 (^)
- 逗號運算符 (,)
- 比較運算符
- 復合賦值運算符
- 條件(三目)運算符 (?:)
- delete 運算符
- 除法賦值運算符 (/=)
- 除法運算符 (/)
- in 運算符
- 遞增 (++) 和遞減 (--) 運算符
- instanceof 運算符
- 左移賦值運算符 (<<=)
- 邏輯“與”運算符 (&&)
- 邏輯“非”運算符 (!)
- 邏輯“或”運算符 (||)
- 取余賦值運算符 (%=)
- 取余運算符 (%)
- 乘法賦值運算符 (*=)
- 乘法運算符 (*)
- new 運算符
- 右移賦值運算符 (>>=)
- 減法賦值運算符 (-=)
- 減法運算符 (-)
- typeof 運算符
- 無符號右移運算符 (>>>)
- 無符號右移賦值操作 (>>>=)
- void 運算符
- JScript 屬性
- $1...$9 屬性
- arguments 屬性
- caller 屬性
- constructor 屬性
- description 屬性
- E 屬性
- global 屬性
- hasOwnProperty 方法
- ignoreCase 屬性
- index 屬性
- Infinity 屬性
- input 屬性($_)
- isProptotyeOf 方法
- lastIndex 屬性
- length 屬性 (Array)
- length 屬性 (Function)
- length 屬性 (String)
- LN10 屬性
- LN2 屬性
- LOG10E 屬性
- LOG2E 屬性
- MAX_VALUE 屬性
- message 屬性
- MIN_VALUE 屬性
- multiline 屬性
- name 屬性
- NaN 屬性
- NaN 屬性 (Global)
- NEGATIVE_INFINITY 屬性
- number 屬性
- PI 屬性
- POSITIVE_INFINITY 屬性
- propertyIsEnumerable 屬性
- prototype 屬性
- source 屬性
- SQRT1_2 屬性
- SQRT2 屬性
- undefined 屬性
- JScript 語句
- @cc_on 語句
- @if 語句
- @set 語句
- break 語句
- Comment 語句
- continue 語句
- do...while 語句
- for 語句
- for...in 語句
- function 語句
- if...else 語句
- Labeled 語句
- return 語句
- switch 語句
- this 語句
- throw 語句
- try...catch...finally 語句
- var 語句
- while 語句
- with 語句
- FileSystemObject 用戶指南
- FileSystemObject 對象模型
- FileSystemObject 和 Scripting 運行時庫參考的介紹
- FileSystemObject 對象
- 設計 FileSystemObject
- 處理驅動器和文件夾
- 處理文件
- FileSystemObject 示例代碼
- Scripting 運行時庫參考
- 腳本運行時方法
- Add 方法 (Dictionary)
- Add 方法 (Folders)
- BuildPath 方法
- Close 方法
- Copy 方法
- CopyFile 方法
- CopyFolder 方法
- CreateFolder 方法
- CreateTextFile 方法
- Delete 方法
- DeleteFile 方法
- DeleteFolder 方法
- DriveExists 方法
- Exists 方法
- FileExists 方法
- FolderExists 方法
- GetAbsolutePathName 方法
- GetBaseName 方法
- GetDrive 方法
- GetDriveName 方法
- GetExtensionName 方法
- GetFile 方法
- GetFileName 方法
- getFileVersion 方法
- GetFolder 方法
- GetParentFolderName 方法
- GetSpecialFolder 方法
- GetTempName 方法
- Items 方法
- Keys 方法
- Move 方法
- MoveFile 方法
- MoveFolder 方法
- OpenAsTextStream 方法
- OpenTextFile 方法
- Read 方法
- ReadAll 方法
- ReadLine 方法
- Remove 方法
- RemoveAll 方法
- Skip 方法
- SkipLine 方法
- Write 方法
- WriteBlankLines 方法
- WriteLine 方法
- 腳本運行時對象
- Dictionary 對象
- Drive 對象
- Drives 集合
- File 對象
- Files 集合
- FileSystemObject 對象
- Folder 對象
- Folders 集合
- TextStream 對象
- 腳本運行時屬性
- AtEndOfLine 屬性
- AtEndOfStream 屬性
- Attributes 屬性
- AvailableSpace 屬性
- Column 屬性
- CompareMode 屬性
- Count 屬性
- DateCreated 屬性
- DateLastAccessed 屬性
- DateLastModified 屬性
- Drive 屬性
- DriveLetter 屬性
- Drives 屬性
- DriveType 屬性
- Files 屬性
- FileSystem 屬性
- FreeSpace 屬性
- IsReady 屬性
- IsRootFolder 屬性
- Item 屬性
- Key 屬性
- Line 屬性
- Name 屬性
- ParentFolder 屬性
- Path 屬性
- RootFolder 屬性
- SerialNumber 屬性
- ShareName 屬性
- ShortName 屬性
- ShortPath 屬性
- Size 屬性
- SubFolders 屬性
- TotalSize 屬性
- Type 屬性
- VolumeName 屬性
- 正則表達式簡介
- 正則表達式
- 早期起源
- 使用正則表達式
- 正則表達式語法
- 建立正則表達式
- 優先權順序
- 普通字符
- 特殊字符
- 非打印字符
- 字符匹配
- 限定符
- 定位符
- 選擇和編組
- 后向引用