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

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

 如上面提到的,我们可以使用下面的脚本来访问Folder对象

Dim objfso,objset
'创建FileSystemObject对象
Set objfso = createobject("Scripting.FileSystemObject")
'返回C:/testing的Folder的对象
Set objset = objfso.GetFolder("C:/testing")

注:通过Folder对象我们就可以访问其提供的方法及属性
Drive、DateCreated、Name属性的使用
示例:

Dim sourcepath
sourcepath = "C:/testing"
Call FolderAttributes(sourcepath)
 
Sub FolderAttributes(sourcepath)
   Dim fso,objset
       Set fso = CreateObject("Scripting.FileSystemObject")
       '使用GetFolder方法来返回Folder对象
       Set objset = fso.getFolder(sourcepath)
       '通过Folder对象来访问Folder属性
       reporter.ReportEvent micDone ,"测试Folder属性","文件夹所在的磁盘为:" & objset.Drive & " 文件夹所创建的日期:" & objset.DateCreated & " 文件夹的名字为:" & objset.Name
End Sub

SubFolers属性的使用

示例:

Dim sourcepath
sourcepath = "C:/Program Files/Common Files"
Call FolderAttributes(sourcepath)
 
Sub FolderAttributes(sourcepath)
   Dim fso,objset,objFolders,FolderName,i
   i = 0
       Set fso = CreateObject("Scripting.FileSystemObject")
       '使用GetFolder方法来返回Folder对象
       Set objset = fso.getFolder(sourcepath)
       'SubFolders属性将返回所有子文件夹对应的Folder集合
       Set objFolders = objset.SubFolders
       '遍历Folder集合,统计出共有多少个文件夹,及相关文件夹的名字
       For Each objFolder in objFolders
              i = i +1
              'vbcr为换行符
              FolderName =  FolderName + objFolder.Name + vbcr
       Next
       msgbox ("共有" & i &"个文件夹,文件夹的名字为:" & FolderName)
End Sub

Files属性的使用

示例:

Dim objFso,objGetFolder,intCount,strFileName
intCount = 0
'创建FileSystemObject对象
Set objFso= CreateObject("Scripting.FileSystemObject")
'使用GetFolder()获得文件夹对象
Set objGetFolder = objFso.GetFolder("C:/test")