分享
 
 
 

FileSystemObject(FSO) 示例代码

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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 DrivesDim DriveDim SSet Drives = FSO.DrivesS = "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, "-") & NewLineFor Each Drive In DrivesS = S & Drive.DriveLetterS = S & TabStop & Drive.PathS = S & TabStop & ShowDriveType(Drive)S = S & TabStop & Drive.IsReadyIf Drive.IsReady Then If DriveTypeNetwork = Drive.DriveType ThenS = S & TabStop & Drive.ShareName ElseS = S & TabStop & Drive.VolumeName End If S = S & TabStop & Drive.FileSystemS = S & TabStop & Drive.TotalSizeS = S & TabStop & Drive.FreeSpaceS = S & TabStop & Drive.AvailableSpaceS = S & TabStop & Hex(Drive.SerialNumber)End IfS = S & NewLineNext GenerateDriveInformation = SEnd Function'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' GenerateFileInformation'' 目的: '' 生成一个字符串,来描述文件的当前状态。'' 示范下面的内容'' - File.Path' - File.Name' - File.Type' - File.DateCreated' - File.DateLastAccessed' - File.DateLastModified' - File.Size' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Function GenerateFileInformation(File)Dim SS = NewLine & "Path:" & TabStop & File.PathS = S & NewLine & "Name:" & TabStop & File.NameS = S & NewLine & "Type:" & TabStop & File.TypeS = S & NewLine & "Attribs:" & TabStop & ShowFileAttr(File)S = S & NewLine & "Created:" & TabStop & File.DateCreatedS = S & NewLine & "Accessed:" & TabStop & File.DateLastAccessedS = S & NewLine & "Modified:" & TabStop & File.DateLastModifiedS = S & NewLine & "Size" & TabStop & File.Size & NewLineGenerateFileInformation = SEnd Function'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' GenerateFolderInformation'' 目的: '' 生成一个字符串,来描述文件夹的当前状态。'' 示范下面的内容'' - Folder.Path' - Folder.Name' - Folder.DateCreated' - Folder.DateLastAccessed' - Folder.DateLastModified' - Folder.Size' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Function GenerateFolderInformation(Folder)Dim SS = "Path:" & TabStop & Folder.PathS = S & NewLine & "Name:" & TabStop & Folder.NameS = S & NewLine & "Attribs:" & TabStop & ShowFileAttr(Folder)S = S & NewLine & "Created:" & TabStop & Folder.DateCreatedS = S & NewLine & "Accessed:" & TabStop & Folder.DateLastAccessedS = S & NewLine & "Modified:" & TabStop & Folder.DateLastModifiedS = S & NewLine & "Size:" & TabStop & Folder.Size & NewLineGenerateFolderInformation = SEnd Function'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' GenerateAllFolderInformation'' 目的: '' 生成一个字符串,来描述一个文件夹和所有文件及子文件夹的当前状态。'' 示范下面的内容'' - Folder.Path' - Folder.SubFolders' - Folders.Count' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Function GenerateAllFolderInformation(Folder)Dim SDim SubFoldersDim SubFolderDim FilesDim FileS = "Folder:" & TabStop & Folder.Path & NewLine & NewLineSet Files = Folder.FilesIf 1 = Files.Count ThenS = S & "There is 1 file" & NewLineElseS = S & "There are " & Files.Count & " files" & NewLineEnd IfIf Files.Count <> 0 ThenFor Each File In FilesS = S & GenerateFileInformation(File)NextEnd IfSet SubFolders = Folder.SubFoldersIf 1 = SubFolders.Count ThenS = S & NewLine & "There is 1 sub folder" & NewLine & NewLineElseS = S & NewLine & "There are " & SubFolders.Count & " sub folders" & NewLine & NewLineEnd IfIf SubFolders.Count <> 0 ThenFor Each SubFolder In SubFoldersS = S & GenerateFolderInformation(SubFolder)NextS = S & NewLineFor Each SubFolder In SubFoldersS = S & GenerateAllFolderInformation(SubFolder)NextEnd IfGenerateAllFolderInformation = SEnd Function'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' GenerateTestInformation'' 目的: '' 生成一个字符串,来描述 C:\Test 文件夹和所有文件及子文件夹的当前状态。'' 示范下面的内容'' - FileSystemObject.DriveExists' - FileSystemObject.FolderExists' - FileSystemObject.GetFolder'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Function GenerateTestInformation(FSO)Dim TestFolderDim SIf Not FSO.DriveExists(TestDrive) Then Exit FunctionIf Not FSO.FolderExists(TestFilePath) Then Exit FunctionSet 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 TestFolderDim SubFolderDim 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.DeleteEnd Sub'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' CreateLyrics'' 目的: '' 在文件夹中创建两个文本文件。''' 示范下面的内容'' - FileSystemObject.CreateTextFile' - TextStream.WriteLine' - TextStream.Write' - TextStream.WriteBlankLines' - TextStream.Close'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Sub CreateLyrics(Folder)Dim TextStreamSet 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.CloseSet 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.CloseEnd Sub'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' GetLyrics'' 目的: '' 显示 lyrics 文件的内容。''' 示范下面的内容'' - FileSystemObject.OpenTextFile' - FileSystemObject.GetFile' - TextStream.ReadAll' - TextStream.Close' - File.OpenAsTextStream' - TextStream.AtEndOfStream' - TextStream.ReadLine'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Function GetLyrics(FSO)Dim TextStreamDim SDim File' 有多种方法可用来打开一个文本文件,和多种方法来从文件读取数据。' 这儿用了两种方法来打开文件和读取文件:Set TextStream = FSO.OpenTextFile(TestFilePath & "\Beatles\OctopusGarden.txt", OpenFileForReading)S = TextStream.ReadAll & NewLine & NewLineTextStream.CloseSet File = FSO.GetFile(TestFilePath & "\Beatles\BathroomWindow.txt")Set TextStream = File.OpenAsTextStream(OpenFileForReading)Do While Not TextStream.AtEndOfStreamS = S & TextStream.ReadLine & NewLineLoopTextStream.CloseGetLyrics = SEnd 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 TestFolderDim SubFoldersDim SubFolderDim TextStream' 排除(a)驱动器不存在,或(b)要创建的目录已经存在的情况。If Not FSO.DriveExists(TestDrive) ThenBuildTestDirectory = FalseExit FunctionEnd IfIf FSO.FolderExists(TestFilePath) ThenBuildTestDirectory = FalseExit FunctionEnd IfSet TestFolder = FSO.CreateFolder(TestFilePath)Set TextStream = FSO.CreateTextFile(TestFilePath & "\ReadMe.txt")TextStream.WriteLine("My song lyrics collection")TextStream.CloseSet SubFolders = TestFolder.SubFoldersSet SubFolder = SubFolders.Add("Beatles")CreateLyrics SubFolderBuildTestDirectory = TrueEnd Function'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 主程序'' 首先,它创建一个 test 目录,以及一些子文件夹和文件。 ' 然后,它转储有关可用磁盘驱动器和 test 目录的某些信息,' 最后,清除 test 目录及其所有内容。'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Sub MainDim 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 SubEnd IfPrint GenerateDriveInformation(FSO) & NewLine & NewLinePrint GenerateTestInformation(FSO) & NewLine & NewLinePrint GetLyrics(FSO) & NewLine & NewLineDeleteTestDirectory(FSO)End Sub

相信这个例子已经含盖了大部分的FSO的应用,大家可以根据这个例子来修改,得到自己

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

cpplus

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有