從windows 95開始,微軟為我們提供了一個(gè)好用的文件搜索器。使用這個(gè)搜索器,我們可以方便快捷的查找到符合條件的文件。但有時(shí)我們?cè)诔绦蛟O(shè)計(jì)中也需要這一功能,能否在自己的程序中實(shí)現(xiàn)呢?答案當(dāng)然是肯定的。下面我們就來著手設(shè)計(jì)一個(gè)vb程序中的文件搜索器。
首先,進(jìn)入vb中,建立一個(gè)新的表單。在表單上建立一個(gè)驅(qū)動(dòng)器列表框(drivelist),一個(gè)目錄列表框(folderlist),一個(gè)檢查框(subcheck),一個(gè)listview控件(filelist),兩個(gè)文本框(filename,filesize)和兩個(gè)按鈕(cmdsearch,cmdexit)。需要注意的是listview 控件是 mscomctl.ocx 文件中一組 activex 控件的一部分。若要在應(yīng)用程序中使用 listview 控件,必須將 mscomctl.ocx 文件添加到工程中。選擇listview控件,點(diǎn)擊鼠標(biāo)右鍵,在彈出菜單中選擇“屬性”。在屬性窗口的“列首”頁中設(shè)置將要顯示的列,連續(xù)按動(dòng)“插入列”5次。排列好各控件的位置,然后加入以下代碼:
option explicit
dim filesys as scripting.filesystemobject
private sub cmdexit_click()
end
end sub
private sub drivelist_change()
folderlist.path = drivelist
end sub
private sub form_load()
dim i as integer
set filesys = new scripting.filesystemobject
folderlist.path = left(drivelist.drive, 2) & "\"
with filelist.columnheaders.item(1)
.alignment = lvwcolumnleft
.text = "文件名"
.width = 2000
end with
with filelist.columnheaders.item(2)
.alignment = lvwcolumnright
.text = "大小(kb)"
.width = 1500
end with
with filelist.columnheaders.item(3)
.alignment = lvwcolumnleft
.text = "類型"
.width = 1500
end with
with filelist.columnheaders.item(4)
.alignment = lvwcolumnleft
.text = "路徑"
.width = 2000
end with
with filelist.columnheaders.item(5)
.alignment = lvwcolumnleft
.text = "修改時(shí)間"
.width = 2000
end with
end sub
private sub form_unload(cancel as integer)
set filesys = nothing
end sub
private sub cmdsearch_click()
dim sfolderstart as scripting.folder
set sfolderstart = filesys.getfolder(folderlist.path)
filelist.listitems.clear
addfiles sfolderstart
if filelist.listitems.count = 0 then msgbox _
"沒找到任何符合條件的文件!",vbokonly + vbinformation, "提示信息"
end sub
private sub addfiles(sfoldersearch as scripting.folder)
dim sfolder as scripting.folder
dim sfile as scripting.file
dim sngfilesize as single
dim lngsize as long
dim itm as listitem
if subcheck.value = 1 then
if sfoldersearch.subfolders.count then
for each sfolder in sfoldersearch.subfolders
addfiles sfolder
next sfolder
end if
end if
sngfilesize = filesize * 1000
for each sfile in sfoldersearch.files
with sfile
if .size >= sngfilesize and iif(filename.text < > "", _
instr(.name, filename.text) > 0, true) then
set itm = filelist.listitems.add(text:=.name)
lngsize = .size / 1000
with itm
.subitems(1) = format(lngsize, "#,###")
.subitems(2) = sfile.type
.subitems(3) = sfile.parentfolder
.subitems(4) = sfile.datelastmodified
end with
end if
end with
next sfile
set itm = nothing
set sfolder = nothing
end sub
運(yùn)行后,選擇相應(yīng)目錄,再在filename文本框中輸入要查找的文件名或文件名的一部分,然后再在filesize文本框中輸入文件的大小,按下搜索按鈕即可搜索到相應(yīng)文件。當(dāng)然,這只是個(gè)功能較為簡(jiǎn)單的文件搜索器,有興趣的讀者可以使用scripting.filesystemobject對(duì)象的屬性為其添加一些更為有用的功能。
首先,進(jìn)入vb中,建立一個(gè)新的表單。在表單上建立一個(gè)驅(qū)動(dòng)器列表框(drivelist),一個(gè)目錄列表框(folderlist),一個(gè)檢查框(subcheck),一個(gè)listview控件(filelist),兩個(gè)文本框(filename,filesize)和兩個(gè)按鈕(cmdsearch,cmdexit)。需要注意的是listview 控件是 mscomctl.ocx 文件中一組 activex 控件的一部分。若要在應(yīng)用程序中使用 listview 控件,必須將 mscomctl.ocx 文件添加到工程中。選擇listview控件,點(diǎn)擊鼠標(biāo)右鍵,在彈出菜單中選擇“屬性”。在屬性窗口的“列首”頁中設(shè)置將要顯示的列,連續(xù)按動(dòng)“插入列”5次。排列好各控件的位置,然后加入以下代碼:
option explicit
dim filesys as scripting.filesystemobject
private sub cmdexit_click()
end
end sub
private sub drivelist_change()
folderlist.path = drivelist
end sub
private sub form_load()
dim i as integer
set filesys = new scripting.filesystemobject
folderlist.path = left(drivelist.drive, 2) & "\"
with filelist.columnheaders.item(1)
.alignment = lvwcolumnleft
.text = "文件名"
.width = 2000
end with
with filelist.columnheaders.item(2)
.alignment = lvwcolumnright
.text = "大小(kb)"
.width = 1500
end with
with filelist.columnheaders.item(3)
.alignment = lvwcolumnleft
.text = "類型"
.width = 1500
end with
with filelist.columnheaders.item(4)
.alignment = lvwcolumnleft
.text = "路徑"
.width = 2000
end with
with filelist.columnheaders.item(5)
.alignment = lvwcolumnleft
.text = "修改時(shí)間"
.width = 2000
end with
end sub
private sub form_unload(cancel as integer)
set filesys = nothing
end sub
private sub cmdsearch_click()
dim sfolderstart as scripting.folder
set sfolderstart = filesys.getfolder(folderlist.path)
filelist.listitems.clear
addfiles sfolderstart
if filelist.listitems.count = 0 then msgbox _
"沒找到任何符合條件的文件!",vbokonly + vbinformation, "提示信息"
end sub
private sub addfiles(sfoldersearch as scripting.folder)
dim sfolder as scripting.folder
dim sfile as scripting.file
dim sngfilesize as single
dim lngsize as long
dim itm as listitem
if subcheck.value = 1 then
if sfoldersearch.subfolders.count then
for each sfolder in sfoldersearch.subfolders
addfiles sfolder
next sfolder
end if
end if
sngfilesize = filesize * 1000
for each sfile in sfoldersearch.files
with sfile
if .size >= sngfilesize and iif(filename.text < > "", _
instr(.name, filename.text) > 0, true) then
set itm = filelist.listitems.add(text:=.name)
lngsize = .size / 1000
with itm
.subitems(1) = format(lngsize, "#,###")
.subitems(2) = sfile.type
.subitems(3) = sfile.parentfolder
.subitems(4) = sfile.datelastmodified
end with
end if
end with
next sfile
set itm = nothing
set sfolder = nothing
end sub
運(yùn)行后,選擇相應(yīng)目錄,再在filename文本框中輸入要查找的文件名或文件名的一部分,然后再在filesize文本框中輸入文件的大小,按下搜索按鈕即可搜索到相應(yīng)文件。當(dāng)然,這只是個(gè)功能較為簡(jiǎn)單的文件搜索器,有興趣的讀者可以使用scripting.filesystemobject對(duì)象的屬性為其添加一些更為有用的功能。