在VB中直接用ODBCAPI访问数据库
从windows 95开始,微软为我们提供了一个易于使用的文件搜索器。使用这个搜索器,我们可以方便快捷地找到符合条件的文件。但是有时候我们在程序设计中也需要这个功能。在我们自己的程序中能实现吗?当然,答案是肯定的。让我们用vb程序设计一个文件搜索器。
首先,进入vb,创建一个新的表单。在窗体上建立一个drivelist框(drive list),一个directory list框(folderlist),一个check box (check box),一个listview控件(filelist),两个文本框(filename,filesize)和两个按钮(cmdsearch,cmdexit)。请注意,listview控件是mscomctl.ocx文件中一组activex控件的一部分。若要在应用程序中使用listview控件,必须将mscomctl.ocx文件添加到项目中。选择listview控件,单击鼠标右键,然后从弹出菜单中选择属性。设置要在属性窗口的“列标题”页中显示的列,并连续按“插入列”五次。排列控件的位置,然后添加以下代码:
option explicit
dim文件系统作为脚本
private sub cmdexit _ click()
end
end sub
private sub drive list _ change()
folder list . path = drive list
end sub
private sub form _ load()
dim I as integer
set filesys = new scripting . file system object
folder list . path = left(drive list . drive,2)& " \ "
with file list . column headers . item(1)
。alignment = lvwcolumnleft
。text = "文件名"
。width = 2000
end With
With file list . column headers . item(2)
。alignment = lvwcolumnright
。text = "size (kb)"
。width = 1500
end With
With file list . column headers . item(3)
。alignment = lvwcolumnleft
。text = "type"
。width = 1500
end With
With file list . column headers . item(4)
。alignment = lvwcolumnleft
。text = "path"
。width = 2000
end With
With file list . column headers . item(5)
。alignment = lvwcolumnleft
。text = "修改时间"
。width = 2000
以
end sub结束
私有sub form _ unload(cancel as integer)
set filesys = nothing
end sub
private sub cmdsearch _ click()
dim sfolderstart as scripting . folder
set sfolderstart = filesys . get folder(folder list . path)
list . listitems . clear
addfilesfolder start
if file list . list items . count = 0 thenmsgbox _
"找不到符合条件的文件!",vbokonly+vbinformation,"提示信息"
end sub
private sub addfiles(sfolder search as scripting . folder)
dim sfolder as scripting . folder
dim sfile as scripting . file
dim sngfilesize as single
dim lngsize as listitem
if subcheck . value = 1 then
if sfolder search . subfolders . count then
for sfolder search . subfolders中的每个sfolder
addfiles sfolder大小> = sngfilesize和iif(filename.text < > " ",_
instr(。name,filename.text) > 0,true)然后
set ITM = file list . listitems . add(text:=。name)
lngsize =。尺寸/1000
带itm
。子项(1) = format(lngsize," #,# # # "
。子项(2) = sfile.type
。子项(3) = sfile.parentfolder
。subitems(4)= sfile . datelastmodified
End with
End
End with
next file
setitm = nothing
setsfolder = nothing
End sub
运行后,选择相应的目录,然后在文件名文本框中输入要查找的文件大小或部分文件名,然后单击。当然,这只是一个简单的文件搜索器。感兴趣的读者可以通过使用scripting.filesystemobject对象的属性为其添加一些更有用的功能。
0条评论