只要一个文本框,一个按钮,文本框里输入文件名,电击按钮就搜索整个硬盘里是否有指定文件,有的话显示在窗体上。
解决方案 »
- 终于有5个三角了......不知道又到什么时候才能有一个星星....散分
- 文本框应用问题
- vb如何调用 .h以及.lib和.dll文件??
- 我在文本框里输入日语为什么显示乱码?
- 谁给俺推荐一个能做任意大小图标的图标编辑工具,最好带捕捉屏幕功能
- 一个关于如何学习vb的问题,请教高手
- 谁有ADO连接ORACLE,并操作数据的代码,贴出来,给分,急!!!
- 请问:PictureBox的PaintPicture方法和API的StretchBlt有何不同?
- 散分! 今天辞职了,以后就要......
- 怎样用表格控件显示SQL语句生成的临时表????
- 用VB的OLE控件如果显示多页的WORD文档
- 请教各位大侠个VB简单问题哈,谢谢大家帮助~!
*****************************************************************************
欢迎使用CSDN论坛阅读器 : CSDN Reader(附全部源代码)
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
Public Sub ScanFile(ByVal Path As String, Optional ByVal Filter As String = "*.*")
Dim FileName As String
Dim Folder() As String
Dim i As Long, x As Long
Dim str As String
Dim ext() As String, ext2 As String Path = ValidateDir(Path)
ext = Split(Filter, ";") FileName = Dir(Path)
' Debug.Print FileName
Do While FileName <> ""
If InStr(1, Filter, "*.*") Then
str = str & Path & FileName & vbCrLf
Else
ext2 = LCase(GetExtension(FileName))
For i = 0 To UBound(ext)
If LCase(GetExtension(ext(i))) = ext2 Then
str = str & Path & FileName & vbCrLf
Exit For
End If
Next End If
FileName = Dir
Loop
FileName = Dir(Path, vbDirectory)
Do While FileName <> ""
If FileName <> "." And FileName <> ".." Then
' MsgBox GetAttr(Path & FileName) And vbDirectory
If GetAttr(Path & FileName) And vbDirectory Then
x = x + 1
ReDim Preserve Folder(x)
Folder(x) = Path & FileName
End If
End If
FileName = Dir
DoEvents
Loop
For i = 1 To x
ScanFile Folder(i) & "\", Filter
Next
ScanFileName = ScanFileName & str
End Sub
Public Declare Function GetLogicalDrives Lib "kernel32" () As Long
Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Const INVALID_HANDLE_VALUE = -1
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 Type FileTime
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Const MaxLFNPath = 260
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 * MaxLFNPath
cShortFileName As String * 14
End Type
Dim wfd As WIN32_FIND_DATA, hItem&, hFile&
Const vbBackslash = "\"
Const vbAllFiles = "*.*"
Const vbKeyDot = 46
Dim sFind As StringPrivate Sub Command1_Click()
Me.MousePointer = 11
sFind = "test.txt" '要查找的文件名
On Error Resume Next
drvbitmask& = GetLogicalDrives()
If drvbitmask& Then
maxpwr% = Int(Log(drvbitmask&) / Log(2))
For pwr% = 0 To maxpwr%
If (2 ^ pwr% And drvbitmask&) Then
Call SearchDirs(Chr$(vbKeyA + pwr%) & ":\")
End If
Next
End If
Me.MousePointer = 0
MsgBox "查找完毕"
End SubPrivate Sub SearchDirs(curpath$)
Dim dirs%, dirbuf$(), I%
DoEvents
hItem& = FindFirstFile(curpath$ + vbAllFiles, wfd)
If hItem& <> INVALID_HANDLE_VALUE Then
Do
If (wfd.dwFileAttributes And vbDirectory) Then
If Asc(wfd.cFileName) <> vbKeyDot Then
TotalDirs% = TotalDirs% + 1
If (dirs% Mod 10) = 0 Then ReDim Preserve dirbuf$(dirs% + 10)
dirs% = dirs% + 1
dirbuf$(dirs%) = Left$(wfd.cFileName, InStr(wfd.cFileName, vbNullChar) - 1)
End If
ElseIf Not UseFileSpec% Then
TotalFiles% = TotalFiles% + 1
End If
Loop While FindNextFile(hItem&, wfd)
Call FindClose(hItem&)
End If
Call SearchFileSpec(curpath$)
For I% = 1 To dirs%: SearchDirs curpath$ & dirbuf$(I%) & vbBackslash: Next I%
End SubPrivate Sub SearchFileSpec(curpath$) '搜索文件
hFile& = FindFirstFile(curpath$ & sFind, wfd)
If hFile& <> INVALID_HANDLE_VALUE Then
Do
List1.AddItem curpath$ & Left$(wfd.cFileName, InStr(wfd.cFileName, vbNullChar) - 1)
DoEvents
Loop While FindNextFile(hFile&, wfd)
Call FindClose(hFile&)
End If
End Sub
也沒樓主所說的ByRef参数类型不符 錯誤