二、仅列出目标文件夹中所有子文件夹名。(不包括目标文件夹中文件、不包括子文件夹中的文件或子文件夹)

  1. Sub ListFilesTest()

  2. With Application.FileDialog(msoFileDialogFolderPicker)

  3. If .Show Then myPath$ = .SelectedItems(1) Else Exit Sub

  4. End With

  5. If Right(myPath, 1) <> "" Then myPath = myPath & ""

  6. MsgBox ListFolders(myPath)

  7. End Sub

  8. Function ListFolders(myPath$)

  9. Set fso = CreateObject("Scripting.FileSystemObject")

  10. For Each f In fso.GetFolder(myPath).SubFolders

  11. j = j + 1: t = t & vbCr & f.Name

  12. Next

  13. ListFolders = j & " Folders:" & t

  14. End Function

复制代码

和楼上的代码ListFiles相比,差异很小,仅在于:
fso.GetFolder(myPath).Files
fso.GetFolder(myPath).SubFolders

即,把目标文件夹fso.GetFolder(myPath)的属性,
有.Files 所有文件、改为 .SubFolders 所有子文件夹

(0)

相关推荐