作者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
→ Semisphere:用亂數產生15萬組文字檔,過濾含有"123"之檔案 02/03 20:50
→ Semisphere:約5.6秒 02/03 20:50
※ 編輯: Semisphere 來自: 140.115.60.193 (02/03 21:08)
推 chiucs:GREAT! THANKS A LOT! 02/05 09:17