Option Explicit
'===========================================================
' These are the API declarations needed for
' the file searching operations
'===========================================================
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * 260
cAlternate As String * 14
End Type
Private Declare Function FindFirstFile _
Lib "kernel32" Alias "FindFirstFileA" ( _
ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile _
Lib "kernel32" Alias "FindNextFileA" ( _
ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" _
(ByVal hFindFile As Long) As Long
'===========================================================
' End Declarations - Start Procedures
'===========================================================
Private Sub GetFavorites(ByVal oldPath$, ByVal newName$, _
Optional start As Boolean)
Dim fFirst&, fNext&, found$, curPath$
Dim w As WIN32_FIND_DATA
curPath = AddSlash(oldPath) & newName
'The first thing is to add this item as a node.
If start Then
'Note this is the first node added so there is
' no need to identify the relative (first param)
' or relationship (second param)
TreeView1.Nodes.Add , , curPath, newName
Else
'New node identity (key) is a string (3rd param).
' Here we are using curpath (which will be unique).
' The fourth param (newName) is what is displayed
' in the TreeView (not necessarily unique).
TreeView1.Nodes.Add oldPath, tvwChild, _
curPath, newName
End If
'Now we search the node (which is a directory).
' If another directory is found then this Sub is
' called recursively to add that as a node.
DoEvents
fFirst = FindFirstFile(AddSlash(curPath) & "*", w)
If fFirst Then
Do
found = TrimNull(w.cFileName)
If w.dwFileAttributes And _
FILE_ATTRIBUTE_DIRECTORY Then
If found <> "." And found <> ".." Then
'This is the recursive call for
' the sub-directory just found
GetFavorites curPath, found
End If
'Else
'This is where the code will go for
' found files (url's) in later demos.
End If
DoEvents
fNext = FindNextFile(fFirst, w)
Loop While fNext
fNext = FindClose(fFirst)
End If
End Sub
Private Function AddSlash$(ByVal s$)
If Len(s) Then
If Right$(s, 1) <> "\" Then s = s & "\"
End If
AddSlash = s
End Function
Private Function TrimNull$(ByVal s$)
Dim pos As Integer
pos = InStr(s, Chr$(0))
If pos Then s = Left$(s, pos - 1)
TrimNull = s
End Function
Private Sub Command1_Click()
Static working As Boolean
'Do not allow click while loading
If working Then Exit Sub
working = True
'Clear the tree of all nodes
TreeView1.Nodes.Clear
'We have to specify the path and node display
' text to get the ball rolling.
GetFavorites "c:\windows\", "Favorites", True
'Programatically show the first expansion.
' Note there is no 0 node.
TreeView1.Nodes.Item(1).Expanded = True
working = False
End Sub
Private Sub Form_Load()
Move 1000, 1000, 4800, 3600
TreeView1.Move 120, 120, 2300, 2900
Command1.Move 3000, 1080, 1200, 375
Command1.Caption = "Refresh"
End Sub
|