-3
我有下面的代碼列出文件夾和子文件夾中的所有文件與路徑。但我想列出只有特定擴展名的文件(例如:.txt文件)複製具有特定擴展名的文件名的位置
我該怎麼做..?
Code:
Sub Test()
Call ListFilesInFolder("D:\Downloads", True)
End Sub
Sub ListFilesInFolder(ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean)
Dim FSO As Object
Dim SourceFolder As Object
Dim SubFolder As Object
Dim FileItem As Object
Dim r As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FSO.GetFolder(SourceFolderName)
r = Range("A65536").End(xlUp).Row + 1
For Each FileItem In SourceFolder.Files
Cells(r, 1).Formula = FileItem.Name
Cells(r, 2).Formula = FileItem.Path
r = r + 1 ' next row number
X = SourceFolder.Path
Next FileItem
If IncludeSubfolders Then
For Each SubFolder In SourceFolder.SubFolders
ListFilesInFolder SubFolder.Path, True
Next SubFolder
End If
Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
End Sub
你什麼都試過? –
可以使用python直接調用的操作系統創建子文件夾列表及其內容 - 請參見http://superuser.com/a/319009 – JBithell
您可能想使用['os.walk'](https ://docs.python.org/3/library/os.html#os.walk)。 – Matthias