VBS中FileSystemObject对象详解(完整版)

2019-01-15 19:09:06王旭

Dim objfso,objset
'创建FileSystemObject对象
Set objfso = createobject("Scripting.FileSystemObject")
'返回C:/testing的Folder的对象
Set objset = objfso.GetFolder("C:/testing")
' 根据所返回的Folder对象,去访问其相关的属性
reporter.ReportEvent micDone ,"Folder对象的相关属性","文件夹创建的日期:" & objset.DateCreated &"; 文件夹所在的驱动:" & objset.Drive & "; 文件夹的名字:" & objset.Name

注:关于Folder对象的属性,将在下面会详细介绍

GetParentFolderName的使用

方法名:GetParentFolderName(pathspec)
说明:返回字符串,该字符串包含指定的路径中最后一个文件或文件夹的父文件夹。
示例:

Dim objfso
'创建FileSystemObject对象
Set objfso = createobject("Scripting.filesystemobject")
'使用GetParentFolderName方法来返回上一层文件夹
msgbox (objfso.GetParentFolderName("C:/WINDOWS/addins"))

GetSpecialFolder的使用

方法名:GetSpecialFolder(folderspec)
说明:返回指定的特殊文件夹。详细说明请查看表4。
示例:

Dim fso,tempName
Set fso = CreateObject("Scripting.FileSystemObject")
'GetSpecialFolder()中的参数2,返回一个名为Temp的临时文件夹
Set tempName = fso.GetSpecialFolder(2)
msgbox tempName

MoveFolder的使用

方法名:MoveFolder(source,destination)
说明:将一个或多个文件夹从某位置移动到另一位置。详细说明请查看表4。
示例:

Dim sourcepath,destination
sourcepath = "C:/testing"
destination = "C:/123"
'调用MoveFolders方法
call MoveFolders(sourcepath,destination)
 
Sub MoveFolders(sourcepath,destination)
   Dim fso
       '创建FileSystemObject对象
   Set fso = CreateObject("Scripting.FileSystemObject")
       '使用MoveFolder方法,将testing文件夹的内容移动到123文件夹下
   fso.MoveFolder sourcepath,destination
End Sub

注:如果脚本在运行前文件夹123已存在,则运行是会发生错误;在不同的磁盘下MoveFolder方法不能用
       上面提到的Folder方法是基于FileSystemObject 对象的,现在我们来看看基于Folder对象的方法。

Folder对象所提供的方法

Folder 对象提供一组可用于复制、删除和移动当前文件夹的方法。这些方法的运行方式与 FileSystemObject 对象的CopyFolder、DeleFolder 和 MoveFolder 方法相同,但这些方法不要求 source 参数,因为源文件就是这个文件夹。
备注:CopyFolder、DeleFolder等方法是基于FileSystemObject 对象的;而下面的Copy、Delete等方法是基于FileSystemObject对象下的Folder对象,他们实现的功能是类似的。