VB查找并替换文件

来源:百度知道 编辑:UC知道 时间:2024/06/28 09:05:36
在 C:\01 目录及子目录下查找 88.txt (假设88.txt只有一个),然后释放资源101来替换88.txt。
补充一下,是"查找88.txt",前提是不知道88.txt在哪个目录中;所以"If Len(Dir("C:\01\88.txt")) <> 0 Then......"是不行的,因为前提是根本不知道88.txt在哪个目录。有可能是C:\01\AB\88.txt,有可能是C:\01\love\321\88.txt ...... 但无论88.txt在哪个目录, C:\01 目录及所有子目录下,只有唯一一个88.txt.

Private Declare Function SearchTreeForFile Lib "imagehlp.dll" (ByVal lpRoothPath As String, ByVal lpInputName As String, ByVal lpOutputName As String) As Long

Private Function sysFileFind(ByVal WhichRootPath As String, ByVal WhichFileName As String) As String
Dim iNull As Integer
Dim lResult As Long
Dim sBuffer As String
On Error GoTo L_FILEFINDERROR
sBuffer = String$(1024, 0)
lResult = SearchTreeForFile(WhichRootPath, WhichFileName, sBuffer)
If lResult Then
iNull = InStr(sBuffer, vbNullChar)
If Not iNull Then
sBuffer = Left$(sBuffer, iNull - 1)
End If
sysFileFind = sBuffer
Else
sysFileFind = ""
End If
Exit Function
L_FILEFINDERROR:
MsgBox "错误!", vbInformation, "查找文件错误"
sysFileFind = ""
End Function

Private Sub Find88()
Dim byt() As Byte
Dim filename as String
filename=sysFileF