Не удается определить правильный параметр для фильтрации файлов с помощью рекурсивного 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
Я добавил оператор 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
Вау, спасибо, отлично работает. Можно ли также перенести путь в другой столбец?
Конечно. Просто измените спецификацию диапазона «C5» на нужный столбец.
Спасибо, я надеялся иметь возможность выполнить два транспонирования, второе с путем.
добавлено
Folder As String
Не понимаю, как это повлияет на файловый фильтр.