recursion | function
Realization of Recursive search directory with VB function dir
I realized this method a long time ago. It does not take any form of control. Also did not call the system API function Findfirst,findnext to make recursive calls, and others a little different is that I use the Dir () function in VB. In fact, directly using Dir () Functions are not recursive calls of their own, but we can use a way to save the subdirectories of the current search directory, and then recursively call them in their own search (Strpathname) recursive function, so that the specified directory can be searched.
Public Function Getextname (strFileName as String) as String
Dim strtmp as String
Dim Strbyte as String
Dim I as Long
For i = Len (strFileName) to 1 Step-1
Strbyte = Mid (strFileName, I, 1)
If strbyte <> "." Then
strtmp = Strbyte + strtmp
Else
Exit for
End If
Next I
Getextname = strtmp
End Function
Public Function Search (ByVal strpath As String, Optional strsearch As String = "") as Boolean
Dim Strfiledir () as String
Dim strfile as String
Dim I as Long
Dim Ldircount as Long
On Error GoTo MYERR
If Right (strpath, 1) <> "\" Then strpath = strpath + "\"
strfile = Dir (strpath, vbdirectory or Vbhidden or vbnormal)
While strfile <> "" ' Search current directory
DoEvents
if (GetAttr (strpath + strfile) and vbdirectory) = vbdirectory Then ' If directory is found
If strfile <> "." and strfile <> "..." Then ' excludes parent directory (..) and current directory (.)
Ldircount = Ldircount + 1 ' adds directory number 1
ReDim Preserve Strfiledir (ldircount) as String
Strfiledir (lDirCount-1) = Strfile ' Saves the current directory name with a dynamic array
End If
Else
If strsearch = "" Then
Form1.List1.AddItem strpath + strfile
ElseIf LCase (getextname (strpath + strfile)) = LCase (Getextname (strsearch)) Then
' Satisfies the search criteria, the file is processed
Form1.List1.AddItem strpath + strfile ' saves the full name of the file to the list box List1
End If
End If
strfile = Dir
Wend
For i = 0 to LDirCount-1
Form1.Label3.Caption = strpath + strfiledir (i)
Call Search (strpath + strfiledir (i), strsearch) ' recursively search subdirectories
Next
ReDim strfiledir (0) ' Clears the dynamic array
Search = True ' searches succeeded
Exit Function
MYERR:
Search = False ' searches failed
End Function
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.