vb查找文件的一般方法
Option Explicit
Private WithEvents Command1 As CommandButton
Private WithEvents List1 As ListBox
Dim N, Id, FindNumber As Long
Dim FindPath(), FindName As String
Private Sub Command1_Click()
FindName = InputBox("输入搜索的文件名。输入 * 表示所有。", "提示")
If FindName = "" Then Exit Sub
Id = 1
FindNumber = 0
List1.Clear
ReDim FindPath(Id)
FindPath(Id) = InputBox("输入搜索的路径", "提示")
On Error GoTo cw1
If Not (FindPath(Id) Like "*\") Then FindPath(Id) = FindPath(Id) + "\"
SetAttr FindPath(Id), vbNormal
Id = Id + 1
ReDim Preserve FindPath(Id)
On Error GoTo cw2
N = 1
While FindPath(N) <> ""
FindFile (FindPath(N))
N = N + 1
Wend
MsgBox "搜索完毕,已经找到" + Str(FindNumber) + "个相关文件", , "提示"
Exit Sub
cw1:
MsgBox "对不起,您输入的地址无效,请重新输入", , "错误提示"
Exit Sub
cw2:
MsgBox "对不起,在搜索过程中出现错误", , "错误提示"
End Sub
Sub FindFile(Path)
Dim FileName As String
FileName = Dir(Path, 31)
While FileName <> ""
If FileName = "RECYCLER" Or FileName = "System Volume Information" Then
Else
If FileName <> "." And FileName <> ".." Then
If GetAttr(Path + FileName) And vbDirectory Then
FindPath(Id) = Path + FileName + "\"
Id = Id + 1
ReDim Preserve FindPath(Id)
Else
If FindName = "*" Then
List1.List(List1.ListCount) = Path + FileName
FindNumber = FindNumber + 1
Else
If InStr(FileName, FindName) <> 0 Then
List1.List(List1.ListCount) = Path + FileName
FindNumber = FindNumber + 1
End If
End If
End If
End If
End If
FileName = Dir
DoEvents
Wend
End Sub
Private Sub Form_Load()
Dim X0 As Long
Dim Y0 As Long
Set Command1 = Controls.Add("vb.CommandButton", "command1")
Set List1 = Controls.Add("vb.listbox", "list1")
Form1.Caption = "FindWork"
Form1.Height = 7515
Form1.Width = 5025
X0 = Screen.Width
Y0 = Screen.Height
X0 = (X0 - Me.Width) / 2
Y0 = (Y0 - Me.Height) / 2
Me.Move X0, Y0 '让窗体居中
List1.Height = 6360
List1.Left = 120
List1.Top = 120
List1.Width = 4695
List1.Clear
List1.Visible = True
Command1.Height = 375
Command1.Left = 120
Command1.Top = 6600
Command1.Width = 4695
Command1.Caption = "Start Find"
Command1.Visible = True
End Sub
Private Sub List1_DblClick()
If MsgBox("是否打开‘" + List1.List(List1.ListIndex) + "’?", vbYesNo, "提示") = vbYes Then Shell "cmd.exe /c start " + List1.List(List1.ListIndex) + """"
End Sub
==================================================================
以查找"D:\"下所有文件夹为例:
在窗体上添加一个CommandButton,它的单击代码如下:
Private Sub Command1_Click()
mypath = "D:\" '将要查找的目录赋值给mypath
MyName = Dir(mypath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly Or vbSystem) '找到第一个入口,DIR返回找到的第一个文件(文件夹)
Do While MyName <> "" '开始循环
If MyName <> "." And MyName <> ".." Then '忽略当前目录
VB查找文件的两种方法_IT/计算机_专业资料。编程笔记VB查找文件的两种方法 --- VB开发人员都会遇到文件定位的问题。VB提供的Dir[(pathname[, attributes])]函数应...
VB 实现文件查找功能 在 VB 中要实现查找文件功能,我们可以利用 VB 的 DIR 函数进行递归来实现。每次使用 DIR 函数后,比较是否有要查找的文件,再检查是否有子...
--- 一. 利用DIR函数查找文件 --- Dir[(pathname[, attributes])]是VB提供的用来检查某些文件或目录是否存在的函数,它返回一个 String,用以表示一个文件名、目...
VB 中遍历目录,遍历目录中查找文件(api 和 dir()) 2007 年 05 月 05 日 星期六 14:59 VB 中遍历目录,遍历目录查找文件的 2 个实现方法 方法 1:API ...
VB查找并替换WORD文档中的字符串_其它语言学习_外语学习_教育专区。VB查找并...Else '咨询是否替换文件,如果不替换则放弃本次操作,否则存在本次操作 If MsgBox...
法 2 dos 命令 其实 dos 命令 dir c:\*\*\*.dwg> list.txt 也可以 法 3 用 VB 函数 Dir 实现递归搜索目录 '函数 GetExtName '功能:得到文件后缀名(...
? ? ? ? vb 检查文件是否存在 如何用 VB 检查一网上文件是否存在 如何用 ...满足搜索条件,则处理该文件 Form1.List1.AddItem strPath + strFile '将文件...
查找下一个目录 Loop MsgBox c Combo1.ListIndex = 0 '选择第一项 End Sub Re:vb获取目录下所有文件夹名称的方法语法 Dir[(pathname[, attributes])] Dir ...
VB INET 获取文件列表_IT/计算机_专业资料。执行后就一直是“正在向主机发送请求”的状态代码如下…… Private Sub Cmdlist_Click() Inet1.RemoteHost = Txtftp...
VB查找并替换WORD文档中的字符串.txt台湾一日不收复,我一日不过4级!如果太阳...Else '咨询是否替换文件,如果不替换则放弃本次操作,否则存在本次操作 If MsgBox...
热门文档