递归程序的问题???

Skylights 2004-08-25 04:54:38
下面一个统计文件总大小的函数~~
请各位大哥看看有没有什么问题~运行起来总是死循环~

Private Function countfilesize(ByVal path As String) As Long
Dim fd As WIN32_FIND_DATA
Dim hfile As Long
Dim fsize As Long

hfile = FindFirstFile(path & "\*.*", fd)
Do
Debug.Print fd.cFileName
If fd.dwFileAttributes <> FILE_ATTRIBUTE_DIRECTORY And fd.dwFileAttributes <> FILE_ATTRIBUTE_SYSTEM _
And fd.dwFileAttributes <> FILE_ATTRIBUTE_HIDDEN Then
fsize = fsize + fd.nFileSizeLow
End If
If fd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY And Left(fd.cFileName, 1) <> "." Then
fsize = fsize + countfilesize(path & "\" & fd.cFileName)
End If
Loop While (FindNextFile(hfile, fd) > 0)
Call FindClose(hfile)

End Function
...全文
136 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Skylights 2004-08-25
  • 打赏
  • 举报
回复
谢谢 楼上的,我知道怎么回事了~~
又是 API 的char数组 和 VB string 转换的问题~
northwolves 2004-08-25
  • 打赏
  • 举报
回复
你也可以用FSO 获得文件夹的大小:

Private Sub Command1_Click()
Dim fso As Object, fld As folder
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.getfolder("d:\wave")
MsgBox fld.Size & " byte"
End Sub
northwolves 2004-08-25
  • 打赏
  • 举报
回复
这是我收藏的代码,你比较一下:

Option Explicit

Private Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" _
(ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long

Private Declare Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileA" _
(ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long

Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long

Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const MAX_PATH = 260

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

Private Function GetDirectorySize(fld As String) As Currency
'useage: cSize = GetDirectorySize(Foldername) Note cSize needs to be Dimmed As Currency

Dim fHandle As Long

Dim FileName As String

Dim bRet As Boolean

Dim findData As WIN32_FIND_DATA

On Error Resume Next

If Right(fld, 1) <> "\" Then fld = fld & "\" 'add a trailing \ if there isn't one

fHandle = FindFirstFile(fld & "*", findData) 'find the first file/folder in the root path

'get rid of the nulls
FileName = findData.cFileName
FileName = Split(FileName, Chr(0))(0)

'loop until there's nothing left
Do While Len(FileName) <> 0
'if this is a subfolder, drop into it
If (findData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY _
And FileName <> "." And FileName <> ".." Then
'look ma, we're recursing!
GetDirectorySize = GetDirectorySize + GetDirectorySize(fld & "\" & FileName)
End If

'add the bytes to the total
GetDirectorySize = GetDirectorySize + FileLen(fld & FileName)

'get the next one
bRet = FindNextFile(fHandle, findData)

'nothing left in this folder so get out
If bRet = False Then
Exit Do
End If

'get rid of the nulls
FileName = findData.cFileName
FileName = Split(FileName, Chr(0))(0)

'it runs a bit faster without this DoEvents, but if you anticipate using
'it on folders with tons (thousands and thousands) of files and subfolders,
'you might want to uncomment it so your app doesn't appear to be 'frozen'

'DoEvents
Loop

'clean up the file handle
bRet = FindClose(fHandle)

End Function

Private Sub Command1_Click()
MsgBox GetDirectorySize("d:\wave")
End Sub

7,763

社区成员

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

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