含目錄底下的子目錄,求得其所有文件大小之總和其第二個三數的目的是起始Static型態的totbyte,呼叫它時,請不要傳三數進去
PrivateSubCommand1_Click()
DimtotAsLong
tot=GetDirTotalByte("c: ools")
Debug.Printtot
EndSub
PrivateFunctionGetDirTotalByte(CurrentPathAsString,OptionaliAsLong)AsLong
StatictotbyteAsLong
DimnIAsInteger,nDirectoryAsInteger
DimsFileNameAsString,sDirectoryList()AsString
注釋:Initialtotbyte,ifitisnottheRecursivecallthefunction
Ifi<>1Then
totbyte=0
EndIf
注釋:Firstlistallnormalfilesinthisdirectory
sFileName=Dir(CurrentPath,vbNormal vbHidden vbReadOnly vbSystem vbArchive)
DoWhilesFileName<>""
totbyte=totbyte FileLen(CurrentPath sFileName)
sFileName=Dir
Loop
注釋:Nextbuildtemporarylistofsubdirectories
sFileName=Dir(CurrentPath,vbDirectory)
DoWhilesFileName<>""
注釋:Ignorecurrentandparentdirectories
IfsFileName<>"."AndsFileName<>".."Then
注釋:Ignorenondirectories
IfGetAttr(CurrentPath&sFileName)_
AndvbDirectoryThen
nDirectory=nDirectory 1
ReDimPreservesDirectoryList(nDirectory)
sDirectoryList(nDirectory)=CurrentPath&sFileName
EndIf
EndIf
sFileName=Dir
Loop
注釋:Recursivelyprocesseachdirectory
FornI=1TonDirectory
GetDirTotalBytesDirectoryList(nI)&"",1
NextnI
GetDirTotalByte=totbyte
EndFunction->