|
楼主 |
发表于 2016-5-24 16:08
|
显示全部楼层
函数_获取文件夹文件和文件夹清单.rar
(1.91 KB, 下载次数: 185)
- '*********************************
- '******* 北极狐工作室出品 ******
- '******* QQ:14885553 ******
- '*********************************
- Sub Opiona()
- 'On Error Resume Next '// 发生错误,自动执行下一句,就是忽略错误
- Application.ScreenUpdating = False '//关闭屏幕刷新
- Application.DisplayAlerts = False '//关闭系统提示
- t = Timer '//开始时间
- Set SH0 = Sheets("Sheet1")
- SH0.Range("A2:Z65536").ClearContents
-
- FileArr = FileAllArr(ThisWorkbook.Path, "*.xls?", ThisWorkbook.Name, True, False)
- For i = 0 To UBound(FileArr)
-
- msgbox GetPathFromFileName(FileArr(i))
-
- Next i
- Application.ScreenUpdating = True '//恢复屏幕刷新
- Application.DisplayAlerts = True '//恢复系统提示
- MsgBox "一共用时:" & Format(Timer - t, "#0.0000") & " 秒", , "北极狐提示!!" '//提示所用时间
- End Sub
- '*******************************************************************************************************
- '功能: 查找指定文件夹含子文件夹内所有文件名或文件夹名(含路径)
- '函数名: FileAllArr
- '参数1: Filename 需查找的文件夹名,不包含文件名
- '参数2: FileFilter 需要过滤的文件名,可省略,默认为:[*.*]
- '参数3: Liwai 剔除例外的文件名,可省略,默认为:空,一般为:ThisWorkbook.Name
- '参数4: SubFiles 是否需要查找子文件夹内文件,可省略,默认为:true
- '参数5: Files 是否只要文件夹名,可省略,默认为:FALSE
- '返回值: 一个字符型的数组
- '使用方法:FileArr = FileAllArr(ThisWorkbook.Path, "*.xls", ThisWorkbook.Name,false,false)
- '作者: 北极狐工作室 QQ:14885553
- '*******************************************************************************************************
- Public Function FileAllArr(ByVal Filename As String, Optional ByVal FileFilter As String = "*.*", Optional ByVal Liwai As String = "", Optional ByVal SubFiles As Boolean = True, Optional ByVal Files As Boolean = False) As String()
-
- Dim DIC, DID, Ke, MyName, MyFileName
- Dim I As Integer
-
- Set DIC = CreateObject("Scripting.Dictionary") '创建一个字典对象
- Set DID = CreateObject("Scripting.Dictionary")
-
- Filename = Replace(Replace(Filename & "", "\", ""), "\", "")
- DIC.Add (Filename), ""
- I = 0
- Do While I < DIC.Count
- Ke = DIC.keys '开始遍历字典
- If SubFiles = True Then '//如果需要查找子文件夹
- MyName = Dir(Ke(I), vbDirectory) '查找目录
- Do While MyName <> ""
- If MyName <> "." And MyName <> ".." Then
- If (GetAttr(Ke(I) & MyName) And vbDirectory) = vbDirectory Then '如果是次级目录
- DIC.Add (Ke(I) & MyName & ""), "" '就往字典中添加这个次级目录名作为一个条目
- End If
- End If
- MyName = Dir '继续遍历寻找
- Loop
- End If
- I = I + 1
- Loop
- Dim arrx() As String
- I = 0
- If Files = True Then '//是否只输出文件夹名
-
- For Each Ke In DIC.keys '以查找总表所在文件夹下所有excel文件为例
- ReDim Preserve arrx(I)
- If Ke <> Filename Then '//自身文件夹除外
- arrx(I) = Ke
- I = I + 1
- End If
- Next
- FileAllArr = arrx
- Else
- For Each Ke In DIC.keys '以查找总表所在文件夹下所有excel文件为例
- MyFileName = Dir(Ke & FileFilter) '过滤器:EXCEL2003为:*.xls,excel2007为:*.xlsx
- Do While MyFileName <> ""
- If MyFileName <> Liwai Then '排除例外文件
- ReDim Preserve arrx(I)
- arrx(I) = Ke & MyFileName
- I = I + 1
- End If
- MyFileName = Dir
- Loop
- Next
- FileAllArr = arrx
- End If
- End Function
- '*'****************************************************************************************************
- '函数: GetPathFromFileName 根据全路径获得文件名
- '参数1: strFullPath 完整路径
- '参数2: kzm true 返回字符串含扩展名,默认是:False
- '参数3: strSplitor 各级文件夹分隔符
- '作用: 从完整路径获取返回: 文件名(true带扩展名)
- '使用方法: msgbox GetPathFromFileName("C:\windows\text.txt",true)
- '作者: 北极狐工作室 QQ:14885553
- '*'****************************************************************************************************
- Public Function GetPathFromFileName(ByVal strFullPath As String, Optional ByVal kzm As Boolean = False, Optional ByVal strSplitor As String = "") As String
- Dim FileName1 As String
- Dim FNAME As String
- FileName1 = Left$(strFullPath, InStrRev(strFullPath, strSplitor, , vbTextCompare))
- FileName1 = Replace(strFullPath, FileName1, "")
- If kzm = False Then
- GetPathFromFileName = Left(FileName1, InStrRev(FileName1, ".") - 1)
- Else
- GetPathFromFileName = FileName1
- End If
- End Function
复制代码 |
|