Как отфильтровать файл с помощью wscript.shell при заполнении с помощью Application.WorksheetFunction.Transpose (files) - PullRequest
0 голосов
/ 09 апреля 2019

Невозможно определить правильный параметр для фильтра файлов с рекурсивной оболочкой wscript.shell.

Попытка фильтрации в Application.FileDialog завершилась неудачно.Попытался включить расширение .txt после dir, не удалось, по-прежнему извлекает все файлы в рекурсивных каталогах.

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

1 Ответ

1 голос
/ 09 апреля 2019

Я добавил оператор Dim в folder, поэтому в нем не было ошибок. Затем я добавил *.TXT в ваш код WSript. Теперь это возвращает только текстовые файлы.

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
...