• 使用Dir,遍历文件夹下所有子文件夹及文件


    '-------------------------------------------
    '获取某文件夹下所有文件和子目录下的文件
    '-------------------------------------------
    Sub getAllFile()
    
    Cells.ClearContents
    
    Call getFileNm(ChooseFolder(), 0, 0)
    
    MsgBox "处理完成!"
    
    End Sub
    
    '-------------------------------------------
    '获取目标文夹路径
    '-------------------------------------------
    Public Function ChooseFolder() As String
        Dim dlgOpen As FileDialog
        Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
        With dlgOpen
            If .Show = -1 Then
                    ChooseFolder = .SelectedItems(1)
            End If
        End With
        Set dlgOpen = Nothing
    End Function
    
    '-------------------------------------------
    '获取文件夹下所有文件和文件夹名称
    'row worksheet里的打印行
    'col worksheet里的打印列(根据层次显示)
    '------------------------------------------- Public Sub getFileNm(ByVal subFolderPath As String, _ ByRef row As Integer, _ ByVal col As Integer) Dim fileName As String Dim subFolderVisited As String col = col + 1 subFolderPath = subFolderPath & "" fileName = Dir(subFolderPath, vbDirectory) Do While fileName <> "" If fileName <> "." And fileName <> ".." Then If GetAttr(subFolderPath & fileName) And vbDirectory Then
    '文件夹的场合 row = row + 1 Worksheets(1).Cells(row, col) = fileName Call getFileNm(subFolderPath & fileName, row, col) Else '文件的场合 row = row + 1 Worksheets(1).Cells(row, col) = fileName End If End If '获取执行递归前的下一个文件 subFolderVisited = Dir(subFolderPath, vbDirectory) Do While subFolderVisited <> fileName subFolderVisited = Dir Loop fileName = Dir Loop End Sub
  • 相关阅读:
    rgw
    comm命令
    s3cmd、aws的使用
    【RPM包的制作】
    【C++高级编程 | 23】future、packaged_task等使用机制
    【shell语法 | 01】基础练习
    【宏 | 01】#、##、__VA_ARGS__和##__VA_ARGS__的作用
    求求你别再用offset和limit分页了
    你和阿里程序员的差距在哪里?看看鸿蒙级计算机底层知识总结与操作系统就知道了
    什么?CPU 怎么运行代码?太刁难人了吧!
  • 原文地址:https://www.cnblogs.com/changxinblog/p/5624404.html
Copyright © 2020-2023  润新知