Recursive search directory with VB function DIR
I have achieved this method for a long time. It does not use any control form. There is no calling system API function FindFirst, FindNext to recursively call, and someone is a bit different from others is the DIR () function in VB. In fact, directly using the DIR () function is a call that cannot be recursive, but we can use a way to save the subdirectory of the current search directory, then in its own search (strpathname) recursive function The recursive call is followed, so you can search the specified directory.
The specific code is as follows:
'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '
'Function getextname
'Features: Get the file suffix name (extension)
'Enter: file name
'Output: file suffix name (extension)
'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '
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 = strTmpEnd FunctionPublic 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 or vbreadonly) While Strfile <> "" Search Current Directory DOEvents IF (Getattr (StrPath Strfile) and vbdirectory = vbdirectory life 'If found is the directory IF strfile <> "". "And Strfile <>" .. "The' excludes the lift (..) and current Directory (.) LDIRCOUNT = LDIRCOUNT 1 'increases the number of directory 1 Redim Preserve StrfileDir (LDIRCOUNT) AS STRING STRING STRING ir (lDirCount - 1) = strFile 'dynamic array holds the current directory name End If Else If strSearch = "" Then Form1.List1.AddItem strPath strFile ElseIf LCase (GetExtName (strPath strFile)) = LCase (GetExtName (strSearch) "Then 'meets the search criteria, handle the file1.list1.addItem strPath strfile' Save the full name 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