如何用VB程序提取当前文件夹目录下所有文件名以及超链接
首先在窗体上添加一个List控件和Command按钮,具体程序如下:
’调用API函数,实现超链接
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Sub Command1_Click() '调用
Searchfiles App.Path & "\", "*.*"
End Sub
Private Function Searchfiles(Path As String, Filetype As String)
Dim files() As String '文件路径
Dim folder() As String '文件夹路径
Dim a, b, c As Long
Dim spath As String
spath = Dir(Path & Filetype) '查找第一个文件
Do While Len(spath) '循环到没有文件为止
a = a + 1
ReDim Preserve files(1 To a)
files(a) = Path & spath '将文件目录和文件名组合,并放到数组中
List1.AddItem files(a) '加入到list控件中
spath = Dir '查出下一个文件
'DoEvents
Loop
spath = Dir(Path & "\", vbDirectory) '查找第一个文件夹
Do While Len(spath) '循环到没有文件夹为止
If Left(spath, 1) <> "." And Left(spath, 2) <> ".." Then '跳过当前的目录及上级目录,If GetAttr(Path & "\" & spath) = vbDirectory Then '确定是否是一个文件夹
b = b + 1
ReDim Preserve folder(1 To b)
folder(b) = Path & spath & "\" '将目录和文件夹名称组合形成新的目录,并存放到数组
End If
End If
spath = Dir '寻找下一个文件夹
'DoEvents
Loop
For c = 1 To b '使用递归方法,遍历所有目录
Searchfiles folder(c), Filetype
Next
End Function
Private Sub List1_Click() ’调用shellexecute函数
ShellExecute 0, "open", List1.List(List1.ListIndex), vbNullString, vbNullString, 3 End Sub