|
- Function 栈遍历(pPath As String, pMask As String, pSub As Boolean)
- 'fileNameArr装文件名动态数组,psb子目录开关,pPath搜索起始路径,pMask扩展名(如doc)
- On Error Resume Next
- Dim fileNameArr() As String, DirFile, mf&, pPath1$
- Dim workStack$(), top& 'workstack工作栈,top栈顶变量
- pPath = Trim(pPath)
- If Right(pPath, 1) <> "" Then pPath = pPath & "" ' 对搜索路径加 backslash(反斜线)
- pPath1 = pPath
- top = 1
- ReDim Preserve workStack(0 To top)
- Do While top >= 1
- DirFile = Dir(pPath1 & "*." & pMask)
- Do While DirFile <> ""
- mf = mf + 1
- ReDim Preserve fileNameArr(1 To mf)
- fileNameArr(mf) = pPath1 & DirFile
- DirFile = Dir
- Loop
- If pSub = False Then Exit Function
- DirFile = Dir(pPath1, vbDirectory) ' 搜索子目录
- Do While DirFile <> ""
- If DirFile <> "." And DirFile <> ".." Then
- If (GetAttr(pPath1 & DirFile) And vbDirectory) = vbDirectory Then
- workStack(top) = pPath1 & DirFile & "" '压栈
- top = top + 1
- If top > UBound(workStack) Then ReDim Preserve workStack(0 To top)
- End If
- End If
- DirFile = Dir 'next file
- Loop
- If top > 0 Then pPath1 = workStack(top - 1): top = top - 1 '弹栈
- Loop
- 栈遍历 = fileNameArr
- End Function
复制代码
使用:
sub test()
arr=("C:\文档保存", "doc", True)
for each fname in arr
这里放循环处理代码就可以了。
next
end sub
|
|