标题: 关于替代FILESEARCH编码的疑问。 [打印本页] 作者: williamwangc 时间: 2012-3-25 09:42 标题: 关于替代FILESEARCH编码的疑问。 最近在研究TREEVIEW和IMAGELIST,要用到FILESEARCH,由于2007版无这代码,我写了一小段试验FILESEARCH编码的替代编码。
使用DIR来编写的。但不知道为什么少了一半。
Dim str_path As String
Dim str_IcoPath As String
Dim i As Integer
str_path = CurrentProject.Path & "\icon\"
str_IcoPath = str_path & Dir(str_path & "*.ico")
i = 1
Do While Dir <> ""
str_IcoPath = str_path & Dir
Debug.Print str_IcoPath
i = i + 1
Loop
Debug.Print i
问题1:
Option Compare Database
Option Base 1
'strFile()为文件名的数组
Dim strFile() As String
'iFile为文件数量
Dim iFile As Integer
Function FilesSearch(FolderPath As String, Key As String) As bolean
Dim fso As FileSystemObject
Dim aFolder As folder
Set fso = New FileSystemObject
'如果指定路径文件夹不存在,FilesSearch=False
If fso.FolderExists(FolderPath) = False Then
strFile() = ""
FilesSearch = False
Exit Function
Else
Set aFolder = fso.GetFolder(FolderPath)
'如果指定文件类型不存在,FilesSearch=False
If fso.FileExists(FolderPath & "\" & "*." & Key) = False Then
strFile() = ""
FilesSearch = False
Exit Function
Else
'如指定文件类型存在,FilesSearch=True,iFile累加,文件名加入数组
For Each File In aFolder.Files
If File.Name Like FolderPath & "\" & "*." & Key Then
ReDim Preserve strFile(iFile)
strFile(iFile) = aFolder.Files(iFile).Name
iFile = iFile + 1
End If
Next File
FilesSearch = True
End If
End If
End Function