FSO(File System Object)对象是VB6新增的对象。
FOS对象模型包括下表这些对象,它主要对文件系统进行操作(取驱动器信息;取文件、文件夹信息;文件移动、拷贝、删除;文件夹移动、拷贝、删除等等),大大方便我们编程。
对象描述
Drive允许收集关于系统所用的驱动器的信息,诸如驱动器有多少可用空间,其共享名称是什么,等等。请注意,一个“驱动器”并不一定是一个硬盘。它可以是CD-ROM驱动器、一个RAM盘等等。而且,驱动器不一定是和系统物理地连接;也可以通过一个LAN进行逻辑地连接。
Folder允许创建、删除或移动文件夹,并向系统查询文件夹的名称、路径等。
Files允许创建、删除或移动文件,并向系统查询文件的名称、路径等。
FileSystemObject该组的主要对象,提供一整套用于创建、删除、收集相关信息、以及通常的操作驱动器、文件夹和文件的方法。与本对象相关联的很多方法复制了其它对象中的方法。
TextStream允许您读和写文本文件。
FSO对象模型包含在一个称为Scripting的类型库中,此类型库位于Scrrun.Dll文件中。使用前必需将其引用到工程中。请从“工程”菜单的“引用”对话框选择“MicrosoftScriptingRuntime”项。这样就可在工程里使用FSO对象模型中包含的对象。
为了进一步的了解FOS对象的用法,下面将举例子进行详细的说明:
首先新建一个工程,从“工程”菜单的“引用”对话框选择“Microsoft Scripting Runtime”项。在FORM1窗口加上几个控件(如图1所示),其属性设置见下表:
对象 属性 设置
Form Caption Form1
Frame Caption Frame1
TextBox Name Text1
TextBox Name Text2
TextBox Name Text3
TextBox Name Text4
TextBox Name Text5
CommandButton Name Command1
Caption getdrive
CommandButton Name Command2
Caption getfolder
CommandButton Name Command3
Caption copy
CommandButton Name Command4
Caption move
CommandButton Name Command5
Caption delete
Label Caption 路径:
Label Caption 源:
Label Caption 目标:
Label Caption 删除:
OptionButton Name Option1
Caption 对文件夹操作
Value True
OptionButton Name Option2
Caption 对文件操作
Value Fale
在Form1窗口布置好控件,且设置好属性后,将下面的代码写到代码窗口。
Private Sub Command1_Click() '取驱动器信息
On Error Resume Next '如果出错,继续往下执行
Dim fostem As FileSystemObject '文件系统对象
Dim drivelist As Drives '驱动器集合对象
Dim drivetem As Drive '驱动器对象
Dim ss As String
Set fostem = New FileSystemObject '为该对象创建一个实例
Set drivelist = fostem.Drives '取驱动器集合
Text1.Text = ""
Form1.MousePointer = 11 '显示漏斗鼠标
For Each drivetem In drivelist '列举"驱动器集合"里面的"驱动器"
ss = ""
ss = "(" & drivetem.DriveLetter & ")" & " -- " '取出驱动器字母
If drivetem.DriveType = 3 Then '如果是网络驱动器,则将网络驱动器名赋给N
n = drivetem.ShareName
Else '反之将卷名赋给N
n = drivetem.VolumeName
End If
ss = n & ss
ss = ss & "总字节(KB):" & FormatNumber(drivetem.TotalSize / 1024, 0) & "--" '取总字节
ss = ss & "可用空间(KB):" & FormatNumber(drivetem.FreeSpace / 1024, 0) '取可用空间
Text1.Text = Text1.Text & ss & vbCrLf
Next
Form1.MousePointer = 0 '恢复默认的鼠标型状
End Sub
Private Sub Command2_Click() '取文件夹内容
On Error GoTo error1 '如出错,转到 ERROR1 处执行
Dim fostem As FileSystemObject
Dim folderstem As Folders
Dim foldertem As Folder
Dim filerstem As Files
Dim filertem As File
Set fostem = New FileSystemObject
Set foldertem = fostem.GetFolder(Text2.Text) '取文件夹
Set folderstem = foldertem.SubFolders '取子文件夹(集合)
Set filerstem = foldertem.Files '取文件(集合)
Text1.Text = "---------文件夹:----------" & vbCrLf
Form1.MousePointer = 11
For Each foldertem In folderstem '列举文件夹(集合)里面的文件夹
ss1 = foldertem.Name '取文件夹名
Text1.Text = Text1.Text & ss1 & vbCrLf
Next
Text1.Text = Text1.Text & "---------文件:---------" & vbCrLf
For Each filertem In filerstem '列举文件(集合)里面的的文件
ss2 = filertem.Name & "--" '取文件名
ss2 = ss2 & FormatNumber(filertem.Size / 1024, 0) & "(KB)" & " --" '取文件大小
ss2 = ss2 & filertem.Type '取文件类型
Text1.Text = Text1.Text & ss2 & vbCrLf
Next
Form1.MousePointer = 0
ExIT Sub
error1:
MsgBox Err.Description, vbOKOnly + vbExclamation, "警告"
Form1.MousePointer = 0
End Sub
Private Sub Command3_Click() '拷贝操作
On Error GoTo error1
Dim fos As FileSystemObject
Set fos = New FileSystemObject
Form1.MousePointer = 11
If Option1.Value = True Then '如果是对文件夹操作
If fos.FolderExists(Text3.Text) = False Then '检测源文件夹是否存在
MsgBox "源路径不存在", vbOKOnly + vbExclamation, "警告"
Form1.MousePointer = 0
ExIT Sub
End If
fos.CopyFolder Text3.Text, Text4.Text '拷贝文件夹
Else '如果是对文件操作
If fos.FileExists(Text3.Text) = False Then '源文件是否存在
MsgBox "源文件不存在", vbOKOnly + vbExclamation, "警告"
Form1.MousePointer = 0
ExIT Sub
End If
If Trim(Text4.Text) = "" Then '目标路径是否为空
MsgBox "目标路径不能为空", vbOKOnly + vbExclamation, "警告"
Form1.MousePointer = 0
ExIT Sub
End If
fos.CopyFile Text3.Text, Text4.Text '拷贝文件
End If
Form1.MousePointer = 0
ExIT Sub
error1:
MsgBox Err.Description, vbOKOnly + vbExclamation, "警告"
Form1.MousePointer = 0
End Sub
Private Sub Command4_Click() '移动操作
On Error GoTo error1
Dim fos As FileSystemObject
Set fos = New FileSystemObject
Form1.MousePointer = 11
If Option1.Value = True Then '如果是对文件夹操作
If fos.FolderExists(Text3.Text) = False Then '检测源文件夹是否存在
MsgBox "源路径不存在", vbOKOnly + vbExclamation, "警告"
Form1.MousePointer = 0
ExIT Sub
End If
fos.MoveFolder Text3.Text, Text4.Text '移动文件夹
Else '如果是对文件操作
If fos.FileExists(Text3.Text) = False Then '检测源文件是否存在
MsgBox "源文件不存在", vbOKOnly + vbExclamation, "警告"
Form1.MousePointer = 0
ExIT Sub
End If
If Trim(Text4.Text) = "" Then
MsgBox "目标路径不能为空", vbOKOnly + vbExclamation, "警告"
Form1.MousePointer = 0
ExIT Sub
End If
fos.MoveFile Text3.Text, Text4.Text '移动文件
End If
Form1.MousePointer = 0
ExIT Sub
error1:
MsgBox Err.Description, vbOKOnly + vbExclamation, "警告"
Form1.MousePointer = 0
End Sub
Private Sub Command5_Click() '删除操作
On Error GoTo error1
Dim fos As FileSystemObject
Set fos = New FileSystemObject
Form1.MousePointer = 11
If Option1.Value = True Then '如果是对文件夹操作
fos.DeleteFolder Text5.Text, True '删除文件夹,不管其属性是否是只读
Else '如果是对文件操作
fos.DeleteFile Text5.Text, True '删除文件,不管其属性是否是只读
End If
Form1.MousePointer = 0
ExIT Sub
error1:
MsgBox Err.Description, vbOKOnly + vbExclamation, "警告"
Form1.MousePointer = 0
End Sub
写完代码后,千万别忘记存盘。按F5运行程序,你将会对FOS对象有更深的理解,也体会到它给我们编程带来的方便。