Tutorials to .com

Tutorials to .com » Dotnet » Vb-net » Dir function to use VB to achieve recursive directory search

Dir function to use VB to achieve recursive directory search

Print View , by: iSee ,Total views: 8 ,Word Count: 527 ,Date: Sat, 18 Apr 2009 Time: 5:35 AM


Dir function to use vb to achieve recursive directory search

I long ago realized this way. It does not use any form of control. Do not call the system API function FindFirst, FindNext for recursive calls, and is a bit different from others I have used VB in the Dir () function . In fact, the direct use of Dir () function is not to carry out the recursive call itself, but we can be a way to Dir will search the current directory to save the subdirectory and then in its own search (strPathName) delivery In the recursive function call in order to carry out so that you can search the directory specified by the end.

Specific code is as follows:

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''

'Function GetExtName

'Function: get file name suffix (extension)

'Input: file name

'Output: File name suffix (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 = 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 Or vbReadOnly)
While strFile <> "" 'Search the current directory
DoEvents
If (GetAttr (strPath + strFile) And vbDirectory) = vbDirectory Then 'If the directory to find
If strFile <> "." And strFile <> ".." Then 'exclude the parent directory (..) and the current directory (.)
lDirCount = lDirCount + 1 'to the directory number by 1
ReDim Preserve strFileDir (lDirCount) As String
strFileDir (lDirCount - 1) = strFile 'save the array with the dynamic of the current directory
End If
Else
If strSearch = "" Then
Form1.List1.AddItem strPath + strFile
ElseIf LCase (GetExtName (strPath + strFile)) = LCase (GetExtName (strSearch)) Then
'Meet the search criteria, then processing the file
Form1.List1.AddItem strPath + strFile 'Save the document to the full name in 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) 'dynamic array empty
search = True 'search for success
Exit Function
MyErr:
search = False 'Search failed
End Function


VB.net Articles


Can't Find What You're Looking For?


Rating: 12345.0

Comments

No comments posted.