Files 集合 描述在一个文件夹内的所有 File 对象的集合。说明下面的代码举例说明了如何获得一个 Files 集合,以及如何用 For Each...Next 语句来访问这个集合中的每个File:Sub ShowFolderList(folderspec) Dim fs, f, f1, fc, s Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFolder(folderspec) Set fc = f.Files For Each f1 in fc s = s & f1.name s = s & vbCrLf Next MsgBox s End Sub
Attributes 属性 描述设置或者返回文件或文件夹的属性。读/写或只读,取决于属性。语法object.Attributes [= newattributes]Attributes 属性有下列几个部分:部分 描述 object 必需的。总是某个 File 或者 Folder 对象的名字。 newattributes 可选的。如果提供的话,newattributes 就是所指定 object 的新属性值。 设置newattributes 参数可以是具有下列值中的任意一个或任意的逻辑组合:常数 值 描述 Normal 0 一般文件。未设置属性。 ReadOnly 1 只读文件。属性为读/写。 Hidden 2 隐藏文件。属性为读/写。 System 4 系统文件。属性为读/写。 Volume 8 磁盘驱动器卷标。属性为只读。 Directory 16 文件夹或目录。属性为只读。 Archive 32 自上次备份后已经改变的文件。属性为读/写。 Alias 64 链接或快捷方式。属性为只读。 Compressed 128 压缩文件。属性为只读。 说明下面的代码用一个文件举例说明了 Attributes 属性的用法:Sub SetClearArchiveBit(filespec) Dim fs, f, r Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFile(fs.GetFileName(filespec)) If f.attributes and 32 Then r = MsgBox("The Archive bit is set, do you want to clear it?", vbYesNo, "Set/Clear Archive Bit") If r = vbYes Then f.attributes = f.attributes - 32 MsgBox "Archive bit is cleared." Else MsgBox "Archive bit remains set." End If Else r = MsgBox("The Archive bit is not set. Do you want to set it?", vbYesNo, "Set/Clear Archive Bit") If r = vbYes Then f.attributes = f.attributes + 32 MsgBox "Archive bit is set." Else MsgBox "Archive bit remains clear." End If End If End Sub
FSO 对象模型包括这些对象:对象 描述
Drive 允许收集关于系统所用的驱动器的信息,诸如驱动器有多少可用空间,其共享名称是什么,等等。请注意,一个“驱动器”并不一定是一个硬盘。它可以是 CD-ROM 驱动器、一个RAM 盘等等。而且,驱动器不一定是和系统物理地连接;也可以通过一个 LAN进行逻辑地连接。
Folder 允许创建、删除或移动文件夹,并向系统查询文件夹的名称、路径等。
Files 允许创建、删除或移动文件,并向系统查询文件的名称、路径等。
FileSystemObject 该组的主要对象,提供一整套用于创建、删除、收集相关信息、以及通常的操作驱动器、文件夹和文件的方法。与本对象相关联的很多方法复制了其它对象中的方法。
TextStream 允许您读和写文本文件。
关于 FSO 对象模型中各种属性、方法和事件的信息,请使用 Visual Basic 中的“对象浏览器”(按 F2 键)并查看 Scripting 类型库。
描述在一个文件夹内的所有 File 对象的集合。说明下面的代码举例说明了如何获得一个 Files 集合,以及如何用 For Each...Next 语句来访问这个集合中的每个File:Sub ShowFolderList(folderspec)
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
s = s & f1.name
s = s & vbCrLf
Next
MsgBox s
End Sub
描述设置或者返回文件或文件夹的属性。读/写或只读,取决于属性。语法object.Attributes [= newattributes]Attributes 属性有下列几个部分:部分 描述
object 必需的。总是某个 File 或者 Folder 对象的名字。
newattributes 可选的。如果提供的话,newattributes 就是所指定 object 的新属性值。
设置newattributes 参数可以是具有下列值中的任意一个或任意的逻辑组合:常数 值 描述
Normal 0 一般文件。未设置属性。
ReadOnly 1 只读文件。属性为读/写。
Hidden 2 隐藏文件。属性为读/写。
System 4 系统文件。属性为读/写。
Volume 8 磁盘驱动器卷标。属性为只读。
Directory 16 文件夹或目录。属性为只读。
Archive 32 自上次备份后已经改变的文件。属性为读/写。
Alias 64 链接或快捷方式。属性为只读。
Compressed 128 压缩文件。属性为只读。
说明下面的代码用一个文件举例说明了 Attributes 属性的用法:Sub SetClearArchiveBit(filespec)
Dim fs, f, r
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(fs.GetFileName(filespec))
If f.attributes and 32 Then
r = MsgBox("The Archive bit is set, do you want to clear it?", vbYesNo, "Set/Clear Archive Bit")
If r = vbYes Then
f.attributes = f.attributes - 32
MsgBox "Archive bit is cleared."
Else
MsgBox "Archive bit remains set."
End If
Else
r = MsgBox("The Archive bit is not set. Do you want to set it?", vbYesNo, "Set/Clear Archive Bit")
If r = vbYes Then
f.attributes = f.attributes + 32
MsgBox "Archive bit is set."
Else
MsgBox "Archive bit remains clear."
End If
End If
End Sub