zoukankan      html  css  js  c++  java
  • VB 中定义FileSystemObject对象,要先添加对象

      存取文件的方法有很多种,可以使用上述VB提供的函数,使用Windows API函数等等,但是最简单的方法是使用FileSystemObject对象。

        1、使用FileSystemObject对象   

        FileSystemObject对象并非VB内置的对象,必须引用“Microsoft Scripting Runtime”对象模块,VB程序才可以使用FileSystemObject。

        选取VB菜单的“工程/引用”命令: 

    引用了“Microsoft Scripting Runtime”对象模块后,如果使用“对象浏览器”窗口,可以看到在“Scripting”模块中新增的Dictionary、Drive、Drives、File、Files、FileSystemObject、Folder、Folders、TextStream等对象。见下图:

    其中FileSystemObject是这些对象的关键,要使用其他对象,先必须建立FileSystemObject对象。

        FileSystemObject对象及其子对象

        建立FileSystemObject对象的方法如下:
        Dim 对象变量名 fs As New FileSystemObject

        1、Folder对象与文件夹的浏览
        使用Folder对象可以获得文件夹的信息,例如:
        Dim fs As New FileSystemObject '建立FileSystemObject对象
        Dim fd As Folder '定义Folder对象
        Set fd = fs.GetFolder("c:") '建立c:文件夹所对应的Folder对象

        2、Files集合对象和File对象
        Folderduix d  File属性是一个集合对象,所代表的是该文件夹中所有的File对象的集合。
        Dim f As File '定义File对象
        For Each f In fd.Files '此时f等于被列举的File对象

        下面的代码能在ListBox中显示c:中的所有文件。
        Private Sub Command1_Click()
        Dim fs As New FileSystemObject
        Dim fd As Folder
        Dim f As File '定义File对象
        Set fd = fs.GetFolder("c:")
        For Each f In fd.Files '每次进入循环,f代表一个文件
        List1.AddItem f.Name '显示每个文件后将其名称加入到ListBox之中
        Next
        End Sub

        3、SubFolder集合对象
        除了File属性之外,SubFolder属性也是附属于Folder对象的集合对象,而它所代表的是该文件夹所有Folder对象的集合。

        下面的代码能在ListBox中显示c:中的所有文件夹
        Dim fs As New FileSystemObject
        Dim fd As Folder
        Dim sfd As Folder
        Set fd = fs.GetFolder("d:")
        For Each sfd In fd.SubFolders '每次进入循环,sfd代表一个文件夹
        List1.AddItem sfd.Name '显示每个文件夹后将其名称加入到ListBox之中
        Next

        Folder对象和File对象属性应用

        依靠Folder的File对象属性可以完成一些实用的程序。

        1、如何知道某个文件夹所占用的磁盘空间
        Dim fs As New FileSystemObject
        Dim fd As Folder
        Set fd = fs.GetFolder("C:")
        Debug.Print fd.Size

        2、如何显示文件夹的所有子文件夹和子子文件夹
        使用VB的“递归调用”(Recursive Call)。所谓递归调用,是指子程序(或函数)在运行时调用自己,例如:

        Sub Sub X()
          ……
          SubX  '调用自己
          ……
        End Sub

       先写好一个可以显示所有子文件夹(但不含子子文件夹)的子程序ListFolder
       然后使用“递归调用”,完成显示C盘的所有文件。

        Private Sub Command1_Click()
        Dim fs As New FileSystemObject ' 建立 FileSystemObject
        Dim fd As Folder ' 定义 Folder 对象
        Dim sfd As Folder
        Set fd = fs.GetFolder("c:")
        ListFolder fd
        End Sub

        Sub ListFolder(fd As Folder)
        Dim sfd As Folder
        For Each sfd In fd.SubFolders
        Debug.Print sfd.Path
        List1.AddItem sfd.Path
        ListFolder sfd ' 以 sfd 为参数,递归调用
        Next
        End Sub

    下列是显示符合条件的文件的实例:

    程序运行界面:

    代码如下:

    Private Sub Command1_Click()
    Dim fs As New FileSystemObject
    Dim fd As Folder, f As File
    Dim attr As Long

    ' 组合文件属性值�
    attr = IIf(chkReadOnly.Value = 1, ReadOnly, 0)
    attr = attr + IIf(chkArchive.Value = 1, Archive, 0)
    attr = attr + IIf(chkHidden.Value = 1, Hidden, 0)
    attr = attr + IIf(chkSystem.Value = 1, System, 0)

    List1.Clear
    Set fd = fs.GetFolder("c:")
    For Each f In fd.Files
        If (f.Attributes And attr) = attr Then
           List1.AddItem f.Name
        End If
    Next
    End Sub

  • 相关阅读:
    python接口自动化33-json解析神器jsonpath
    python测试开发django-79.ORM查询之datetime()格式化(extra )
    python测试开发django-78.ORM查询之extra
    python测试开发django-77.ORM如何添加 DateTimeField 不显示毫秒
    python测试开发django-76.ORM查询之Q查询
    python测试开发django-75.ORM根据日期查询(__range)
    Mysql 跨库数据迁移 -- python 脚本
    Mysql 视图
    mysql update select 从查询结果中更新数据
    Elastic Stack 入门
  • 原文地址:https://www.cnblogs.com/rosesmall/p/3325935.html
Copyright © 2011-2022 走看看