3

我目前有这个代码:

Function GetSubDir(ByVal sDir)

Dim oFS As New FileSystemObject
Dim oDir
iCount = 1
Erase subArray()
Set oDir = oFS.GetFolder(sDir)
For Each oSub In oDir.SubFolders
    MsgBox oSubPath
    GetSubDir oSub.Path
    ReDim Preserve subArray(iCount)
    subArray(iCount) = oSub.Path
    iCount = iCount + 1
Next oSub

End Function

有没有办法修改它以获取文件和文件夹?我曾尝试查看 MSDN,但它对我来说很陌生,是简单的语法更改还是整个代码需要返工?谢谢

4

2 回答 2

3

我认为这很有可能 - 此示例代码显示了如何循环文件:

Sub ShowFileList(folderspec)
    Dim fs, f, f1, fc, s
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set fc = f.Files
    For Each f1 in fc
        s = s & f1.name 
        s = s &  vbCrLf
    Next
    MsgBox s
End Sub

只需您需要再添加一个For...Each循环。但是,如果您想将所有文件和所有子文件夹递归处理到最后一层 - 这将需要更多编码。ready-to-go但是,使用 Google 可能会在几分钟内显示大量此类片段。祝你好运!

于 2013-02-26T17:53:06.053 回答
0

函数 GetSubDir(ByVal sDir)

Dim oFS As New FileSystemObject

Dim oDir
Dim oSub As Object
Dim i As Integer

Dim wbName As String

Set oDir = oFS.GetFolder(sDir)
For Each oSub In oDir.SubFolders
    SDCount = SDCount + 1
    ReDim Preserve wbSDName(1 To SDCount)
    wbSDName(SDCount) = oSub.Path
    GetSubDir oSub.Path
Next oSub

结束功能

于 2020-01-03T06:53:49.083 回答