帮忙看一下 findnextfile 函数哪里有错

jxfzcgh 2003-08-20 12:36:28
麻烦看一下下面这个函数,函数功能是用来删除fpath下面所有文件名包括fname字符的文件,用到api文件查找函数 findfirstfile findnextfile,程序第一次运行到Do While FindNextFile(fItem, lpFile) <> 0这句都没有错,得二次循环的时候就出现严重错误,自动退出vb,错误提示“vb6.0.exe出现错误.....”

Public Sub KillCUTFile(Fpath, fName)
Dim lpFile As WIN32_FIND_DATA
Dim fItem As Long
Dim Fdir As String
Dim strfName() As String
Fdir = Fpath
If Right(Fdir, 1) <> "\" Then Fdir = Fdir & "\"
fItem = FindFirstFile(Fdir & fName, lpFile)
If fItem <> INVALID_HANDLE_VALUE Then
ReDim strfName(1)
strfName(0) = Left(lpFile.cFileName, InStr(lpFile.cFileName, vbNullChar) - 1)
Do While FindNextFile(fItem, lpFile) <> 0
ReDim Preserve strfName(UBound(strfName) + 1)
strfName(UBound(strfName) - 1) = Left(lpFile.cFileName, InStr(lpFile.cFileName, vbNullChar) - 1)
Loop
End If
FindClose fItem
End Sub
...全文
72 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
道素 2003-08-20
  • 打赏
  • 举报
回复
一个例子
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Const MAX_PATH = 260
Const MAXDWORD = &HFFFF
Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private 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 * MAX_PATH
cAlternate As String * 14
End Type
Function StripNulls(OriginalStr As String) As String
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If
StripNulls = OriginalStr
End Function

Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
Dim FileName As String ' Walking filename variable...
Dim DirName As String ' SubDirectory Name
Dim dirNames() As String ' Buffer for directory name entries
Dim nDir As Integer ' Number of directories in this path
Dim i As Integer ' For-loop counter...
Dim hSearch As Long ' Search Handle
Dim WFD As WIN32_FIND_DATA
Dim Cont As Integer
If Right(path, 1) <> "\" Then path = path & "\"
' Search for subdirectories.
nDir = 0
ReDim dirNames(nDir)
Cont = True
hSearch = FindFirstFile(path & "*", WFD)
If hSearch <> INVALID_HANDLE_VALUE Then
Do While Cont
DirName = StripNulls(WFD.cFileName)
' Ignore the current and encompassing directories.
If (DirName <> ".") And (DirName <> "..") Then
' Check for directory with bitwise comparison.
If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
dirNames(nDir) = DirName
DirCount = DirCount + 1
nDir = nDir + 1
ReDim Preserve dirNames(nDir)
End If
End If
Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
Loop
Cont = FindClose(hSearch)
End If
' Walk through this directory and sum file sizes.
hSearch = FindFirstFile(path & SearchStr, WFD)
Cont = True
If hSearch <> INVALID_HANDLE_VALUE Then
While Cont
FileName = StripNulls(WFD.cFileName)
If (FileName <> ".") And (FileName <> "..") Then
FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
FileCount = FileCount + 1
List1.AddItem path & FileName
End If
Cont = FindNextFile(hSearch, WFD) ' Get next file
Wend
Cont = FindClose(hSearch)
End If
' If there are sub-directories...
If nDir > 0 Then
' Recursively walk into them...
For i = 0 To nDir - 1
FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
Next i
End If
End Function
Sub Command1_Click()
Dim SearchPath As String, FindStr As String
Dim FileSize As Long
Dim NumFiles As Integer, NumDirs As Integer
Screen.MousePointer = vbHourglass
List1.Clear
SearchPath = Text1.Text
FindStr = Text2.Text
FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
Screen.MousePointer = vbDefault
End Sub


zyl910 2003-08-20
  • 打赏
  • 举报
回复
WIN32_FIND_DATA、findnextfile的定义?
hc_z 2003-08-20
  • 打赏
  • 举报
回复
用CFSO对象吧。
我记得再VC里面就专门有一文件异常类,可见文件操作的异常很多的。
VB中用API函数产生的异常不能捕获的。能不用就不用吧。
我做的一个函数:

'0 ,整个目录
'1,目录下的所有文件
'2,目录下指定扩展名的文件
Public Sub DelAppointFiles(ByVal folder As String, ByVal tp As Integer, Optional ByVal force As Boolean, Optional ByVal extend As String)
On Error GoTo ERR
Dim Cfso As FileSystemObject '´´Á¢Îļþ¶ÔÏó
Dim CFolder As folder
Dim CSubFolder As folder
Dim Cfile As file
Dim strOut() As String

Set Cfso = CreateObject("scripting.filesystemobject")

If Cfso.FolderExists(folder) = False Then GoTo ERR
Set CFolder = Cfso.GetFolder(folder)
If CFolder Is Nothing Then GoTo ERR

If tp = 0 Then 'ɾ³ýÎļþ¼Ð
CFolder.Delete force
ElseIf tp = 1 Then 'ɾ³ýĿ¼ÏÂËùÓÐÎļþ

'ÏÈ´¦ÀíÎļþ
For Each Cfile In CFolder.Files
If Cfile Is Nothing Then Exit For
Cfile.Delete force
Next
'ÔÙ´¦Àí×ÓÎļþ¼Ð
For Each CSubFolder In CFolder.SubFolders
If CSubFolder Is Nothing Then Exit For
DelAppointFiles CSubFolder.Path, tp, force
Next
ElseIf tp = 2 Then 'ɾ³ýÖ¸¶¨À©Õ¹ÃûµÄÎļþ
'ÏÈ´¦ÀíÎļþ
For Each Cfile In CFolder.Files
If Cfile Is Nothing Then Exit For
If extend <> "" Then 'ÓÐÑ¡Ôñɾ³ý
AnalyzeStringEx Cfile.Name, ".", strOut
If UBound(strOut) > 1 Then
If UCase(strOut(UBound(strOut))) = UCase(extend) Then
Cfile.Delete force
End If
End If
Else 'ûÓкó׺µÄ
AnalyzeStringEx Cfile.Name, ".", strOut
If UBound(strOut) = 1 Then
Cfile.Delete force
End If
End If
Next
'ÔÙ´¦Àí×ÓÎļþ¼Ð
For Each CSubFolder In CFolder.SubFolders
If CSubFolder Is Nothing Then Exit For
DelAppointFiles CSubFolder.Path, tp, force, extend
Next
End If


'ÇåÀí
Erase strOut
Set Cfile = Nothing
Set CFolder = Nothing
Set Cfso = Nothing
Exit Sub
ERR:
MsgBox ERR.Description, vbCritical, "ϵͳ´íÎó"
Erase strOut
Set Cfile = Nothing
Set CFolder = Nothing
Set Cfso = Nothing
End Sub

'¹¦ÄÜ:ÓÃÓÚ½«ÓÉijһ¸öÌØÊâ×Ö·û·Ö¿ªµÄ×Ö·û´®·Ö¿ª³É¶Ì×Ö·û´®
'ÊäÈë²ÎÊý:strIn----´ý·Ö¿ªµÄ×Ö·û´®
' strCleft----·Ö¸ô×Ö·û
'Êä³ö²ÎÊý:strOut------·µ»Ø·Ö½âºóµÄ×Ö·ûÊý×é
'**·µ»Ø´Ó1¿ªÊ¼
Public Sub AnalyzeStringEx(ByVal strIn As String, ByVal strCleft As String, strOut() As String)
On Error GoTo ERR
Dim intNumber As Integer '±»·Ö³É¼¸¶Î
Dim intCurrentPos As Integer 'ÏÖÔÚµÄλÖÃ
Dim intPreviousPos As Integer 'ÉϴεÄλÖÃ

ReDim strOut(0)
intNumber = 0

strIn = Trim(strIn)
If strIn = "" Then GoTo ERR
intCurrentPos = InStr(strIn, strCleft)
'Êý×éµÄ³õʼֵ
ReDim strOut(1)
If (intCurrentPos > 0) Then
strOut(1) = Left(Trim(strIn), intCurrentPos - 1)
Else
strOut(1) = strIn
End If

'дÆäËûÊý×éÔªËØ
intNumber = 1
While intCurrentPos <> 0
intNumber = intNumber + 1
ReDim Preserve strOut(0 To intNumber)
intPreviousPos = intCurrentPos
intCurrentPos = InStr(intPreviousPos + 1, strIn, strCleft)
If intCurrentPos <> 0 Then
strOut(intNumber) = Mid((strIn), intPreviousPos + 1, intCurrentPos - intPreviousPos - 1)
Else
strOut(intNumber) = Mid((strIn), intPreviousPos + 1)
End If
strOut(intNumber) = Trim(strOut(intNumber))
Wend
ERR:

End Sub
道素 2003-08-20
  • 打赏
  • 举报
回复
我在我计算机没发现这个问题
你换个路径试试也可能和你本地文件有关
jxfzcgh 2003-08-20
  • 打赏
  • 举报
回复
这个例子看过了
还是看不出哪里有问题,我得定义都是有了的
而且函数可以运行完第一次循环,第二次循环就会自动退出vb
用findfirstfile也可以找到文件

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧