作者Semisphere (所指千歌音之处)
看板Visual_Basic
标题Re: [VB6 ] 档名过滤器
时间Tue Feb 3 20:49:09 2009
※ 引述《chiucs (ChiuCS)》之铭言:
: 我有一个目录,档案数量非常之大,(约15万笔)
: 目录内的档案会不定时增/删,(所以无法在平时先记录档名备用) (但数量都大约维持在15万笔)
: 想写一个程式,方便找寻目录内部的某一个档案(如包含*ABC*)
: 因为档案的开头文字差异性有限,(档名都是以B2开头,MS无法使用B2*ABC*来dir)
: 档名文数字字数又不定(约15至25字完)
: 我使用dir("*.*"),CreateObject("Scripting")方式,
: 在全部的档案中过滤符合的档名(用instr() or Like)
: 速度非常的慢(要好多分钟以上)
: 想请问,是否有更好的方式,可以过滤档名
: 使用API也可以
: (NOTE:我有用网路上freesware的档案总管所提供的档名过滤器功能,
: 它的速度约在数秒,是否有人知道它是如何作到的)
: 感谢各位大大的耐心阅读与回覆
GetSiftFile("C:\Program Files\Microsoft Visual Studio\MyProjects\ewq\", "ABC")
Option Explicit
Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Public Const MAX_PATH = 260
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public 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 * MAX_PATH
cAlternate As String * 14
End Type
Public Function GetSiftFile(cPath As String, cSearch As String) As String()
' GetSiftFile : 回传过滤後之档案名称
' cPath : 资料夹位置
' cSearch : 欲过滤之档案名称
' Declare
Dim hSearch As Long
Dim iState As Long
Dim iNum As Long
Dim sWFD As WIN32_FIND_DATA
Dim cFileName As String
Dim cSiftFile() As String
' Sift
If Right(cPath, 1) <> "\" Then cPath = cPath + "\"
hSearch = FindFirstFile(cPath + "*", sWFD)
iState = -1
iNum = 0
If hSearch <> -1 Then
Do While iState
cFileName = StripNulls(sWFD.cFileName)
If (cFileName <> ".") And (cFileName <> "..") And (GetFileAttributes(cPath & cFileName) <> 16) Then
If InStr(cFileName, cSearch) <> 0 Then
iNum = iNum + 1
ReDim Preserve cSiftFile(1 To iNum)
cSiftFile(iNum) = cFileName
End If
End If
iState = FindNextFile(hSearch, sWFD)
Loop
iState = FindClose(hSearch)
End If
' Return
GetSiftFile = cSiftFile
End Function
Public Function StripNulls(cOriginal As String) As String
If (InStr(cOriginal, Chr(0)) > 0) Then
cOriginal = Left(cOriginal, InStr(cOriginal, Chr(0)) - 1)
End If
StripNulls = cOriginal
End Function
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.115.60.193
1F:→ Semisphere:用乱数产生15万组文字档,过滤含有"123"之档案 02/03 20:50
2F:→ Semisphere:约5.6秒 02/03 20:50
※ 编辑: Semisphere 来自: 140.115.60.193 (02/03 21:08)
3F:推 chiucs:GREAT! THANKS A LOT! 02/05 09:17