含目录底下的子目录,求得其所有文件大小之总和其第二个叁数的目的是起始Static型态的totbyte,呼叫它时,请不要传叁数进去 PrivateSubCommand1_Click() DimtotAsLong tot=GetDirTotalByte("c:\tools\") 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->
|