使用 Application.WorksheetFunction.Transpose(files) 填充时如何使用 wscript.shell 进行文件过滤

How to file filter using wscript.shell when populating with Application.WorksheetFunction.Transpose(files)

无法使用递归 wscript.shell 确定文件过滤器的正确参数。

已尝试在 Application.FileDialog 中过滤,但失败了。 已尝试在目录后包含 .txt 扩展名,失败,仍然检索递归目录中的所有文件。

Sub test()
    Rows("5:" & Rows.Count).ClearContents
    Dim fileSpec As String, files As Variant
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show = -1 Then
            Folder = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
    fileSpec = Folder
    Debug.Print Folder
    ' How to file filter to select only files with a specific *.dbf extension?
    ' How to get the path without the file name and place into another column?
    files = Split(CreateObject("wscript.shell").exec("cmd /c dir " & Chr(34) & fileSpec & Chr(34) & " /b/s ").stdout.readall, vbCrLf)
    ActiveSheet.Range("C5").Resize(UBound(files)).Value = Application.WorksheetFunction.Transpose(files)
End Sub

我在 folder 中添加了一个 Dim 语句,因此没有错误。然后我将 *.TXT 添加到您的 WSript 代码中。现在只有 returns 个文本文件。

Sub test()
    Rows("5:" & Rows.Count).ClearContents
    Dim fileSpec As String
    Dim files As Variant
    Dim folder As Variant

    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show = -1 Then
            folder = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
    fileSpec = folder
    Debug.Print folder
    ' How to file filter to select only files with a specific *.dbf extension?
    ' How to get the path without the file name and place into another column?
    files = Split(CreateObject("wscript.shell").exec("cmd /c dir " & Chr(34) _
            & fileSpec & "\*.txt" & Chr(34) & " /b/s ").StdOut.ReadAll, vbCrLf)
    ActiveSheet.Range("C5").Resize(UBound(files)).Value = _
            Application.WorksheetFunction.Transpose(files)
End Sub