【问题标题】:Get content of the directory using wildcast with FileSystemObject使用通配符和 FileSystemObject 获取目录的内容
【发布时间】:2015-01-01 05:27:30
【问题描述】:

我现在正在使用 Dir 函数来获取目录的内容。

FileSpec = DirPath & "/GenerateID_*_*.zip"
FileName = Dir(FileSpec)

但是 Dir 函数正在锁定文件夹,并且在我关闭 VBA 应用程序之前无法删除该文件夹。在调用 Dir 函数后,我已尝试 ChDir("C:\") 将此指向另一个目录,但它不起作用。

是否可以使用类似 Dir 函数的 FileSystemObject 进行过滤?或者使用 FileSystemObject 获取所有文件,循环每个文件名并检查每个文件将是唯一的选择?

【问题讨论】:

    标签: vba


    【解决方案1】:

    您当然可以使用 FileSystemObject 遍历文件。来自 FileSystemObject 的 Files 属性的 MSDN documentation

    Function ShowFileList(folderspec)
       Dim fso, f, f1, fc, s
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set f = fso.GetFolder(folderspec)
       Set fc = f.Files
       For Each f1 in fc
          s = s & f1.name 
          s = s &   "<BR>"
       Next
       ShowFileList = s
    End Function
    

    这将返回一个由“
    ”分隔的字符串。您可以将其替换为其他内容,例如 vbCrLf 或您喜欢的任何内容,然后根据需要解析字符串。

    【讨论】:

      猜你喜欢
      • 2018-09-23
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 2012-12-03
      • 1970-01-01
      • 2015-12-10
      相关资源
      最近更新 更多