【问题标题】:VBA - list file names of the given extensions in the given folder and its all subfolders down to last levelVBA - 列出给定文件夹及其所有子文件夹中给定扩展名的文件名,直到最后一级
【发布时间】:2021-10-19 02:22:17
【问题描述】:

您好,我正在尝试通过扩展名从多个文件夹及其子文件夹中获取特定文件,但我在执行此任务时遇到了问题。到目前为止我所拥有的是:

Sub ListFiles()

'Declare variables
Dim i As Long

Dim fileName As Variant
fileName = Dir("J:\BREAKDOWNS\*.PDF")

i = 2
While fileName <> ""
Cells(i, 1).Value = Left(fileName, Len(fileName) - 4)
i = i + 1
fileName = Dir
Wend

End Sub

有人可以帮忙吗?

附言

我需要什么以及我目前得到的是

folder = "J:\BREAKDOWNS\*.PDF"
    
sn = Split(CreateObject("wscript.shell").exec("cmd /c Dir """ & folder & ibox & """ /s /a /b").StdOut.ReadAll, vbCrLf)
     
Sheets(1).Cells(2, 1).Resize(UBound(sn) + 1) = Application.Transpose(sn)

但它返回完整地址,我只需要没有扩展名的文件名。

【问题讨论】:

  • 文件扩展名 ?或文件夹中的所有 PDF?
  • 是的,文件的格式,也就是 .pdf 或 . xlsx。我希望从多个文件夹及其子文件夹中获取所有 .pdf 文件的列表,而不是一个文件夹及其子文件夹,也就是来自驱动器 C: 和驱动器 D:
  • stackoverflow.com/questions/68471751/… 有一个示例函数,可用于返回匹配文件对象的集合 - 请参阅 GetMatches
  • 问题太多了,网上的例子大多是垃圾或仅适用于一个文件夹和一级或获取所有文件但无法指定扩展名等。所以我得到的最接近我在 OP 中提供
  • 作为参考,Loop Through All Subfolders Using VBACycle through sub-folders and files in a user-specified root directory 都使用 Scripting.FileSystemObject... 下面的答案使用 wscript.shell 并带有选择多个文件扩展名的选项。

标签: excel vba shell directory


【解决方案1】:

尝试下面的代码,它列出了给定文件夹中给定扩展名的文件名及其所有子文件夹,直到新添加的工作表上的最后一级

致谢:https://www.youtube.com/watch?v=ddA2_SOaq14

Option Explicit

Sub List_File_Names()
'This macro lists file names of the given extensions in the given folder and _
    its all subfolders down to last level on a newly added sheet
'https://stackoverflow.com/questions/68812888/ _
vba-list-file-names-of-the-given-extensions-in-the-given-folder-and-its-all-su

'reference - https://www.youtube.com/watch?v=ddA2_SOaq14
Dim FNameStr As String, ExtStr As String, ExrArr, sn, nWs As Worksheet
Dim regex As Object, mc As Object, f As String, i As Long
Dim fldr As FileDialog

Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
fldr.Show
f = fldr.SelectedItems(1)
f = f & "\"

Set regex = CreateObject("VBScript.regexp")
regex.ignorecase = False
regex.Global = True

ExtStr = InputBox("Enter extensions of filesnames to be listed delimited by comma", _
       Default:=".xlsx,.pdf")
ExrArr = Split(ExtStr, ",")

FNameStr = ""

If ExtStr <> "" Then
    For i = LBound(ExrArr) To UBound(ExrArr)
    FNameStr = FNameStr & (CreateObject("wscript.shell").exec("cmd /c Dir /s /b """ & _
                f & """ | findstr """ & ExrArr(i) & """ ").stdout.readall)
    Next i
Else
    FNameStr = FNameStr & (CreateObject("wscript.shell").exec("cmd /c Dir /s /b """ & _
                f & """").stdout.readall)
End If

regex.Pattern = "\S[^\n]+\\" 'to remove folder names from full file name
sn = Split(Replace(regex.Replace(FNameStr, ""), vbCrLf, "|"), "|")
Set nWs = Worksheets.Add(Before:=Sheets(1))
nWs.Cells(1).Resize(UBound(sn) + 1) = Application.Transpose(sn)

End Sub

【讨论】:

  • 在 if 条件下,您只能在单元格中添加 pdf 和 xls 文件名...比如 ..If Right(oFile.Name, 3) = "PDF" Or Right(oFile.Name, 4) = "XLSX" Then ws.Cells(i, 1).Value = oFile.Name i = i + 1 End If
  • 请仅在第一个位置插入新工作表后尝试第二个代码。否则,它将覆盖现有的第一张纸。
  • 我确实想覆盖值 btw
  • 好的,所以我想出了设置文件夹位置的东西,它实际上将代码缩短了一半。现在你会怎么写 "sn = Split(CreateObject("wscript.shell").exec("cmd /c Dir """ & F & ibox & """ /s /a /b").StdOut.ReadAll, vbCrLf)" 只包含没有扩展名的文件名
  • cmd list 方法往往会返回#N/A。为什么会有问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 2015-02-16
  • 2015-06-10
  • 2016-07-24
  • 1970-01-01
相关资源
最近更新 更多