取得某个目录底下所有文件大小总和 含目录底下的子目录,求得其所有文件大小之总和 其第二个叁数的目的是起始Static型态的totbyte, 呼叫它时,请不要传叁数进去 Private Sub Command1_Click() Dim tot As Long tot = GetDirTotalByte("c:\tools\") Debug.Print tot End Sub Private Function GetDirTotalByte(CurrentPath As String, Optional i As Long) As Long Static totbyte As Long Dim nI As Integer, nDirectory As Integer Dim sFileName As String, sDirectoryList() As String 'Initial totbyte, if it is not the Recursive call the function If i <> 1 Then totbyte = 0 End If 'First list all normal files in this directory sFileName = Dir(CurrentPath, vbNormal + vbHidden + vbReadOnly + vbSystem + vbArchive) Do While sFileName <> "" totbyte = totbyte + FileLen(CurrentPath + sFileName) sFileName = Dir Loop 'Next build temporary list of subdirectories sFileName = Dir(CurrentPath, vbDirectory) Do While sFileName <> "" 'Ignore current and parent directories If sFileName <> "." And sFileName <> ".." Then 'Ignore nondirectories If GetAttr(CurrentPath & sFileName) _ And vbDirectory Then nDirectory = nDirectory + 1 ReDim Preserve sDirectoryList(nDirectory) sDirectoryList(nDirectory) = CurrentPath & sFileName End If End If sFileName = Dir Loop 'Recursively process each directory For nI = 1 To nDirectory GetDirTotalByte sDirectoryList(nI) & "\", 1 Next nI GetDirTotalByte = totbyte End Function
直接条用windows属性窗口Public Type SHELLEXECUTEINFO cbSize As Long fMask As Long hwnd As Long lpVerb As String lpFile As String lpParameters As String lpDirectory As String nShow As Long hInstApp As Long ' Optional fields lpIDList As Long lpClass As String hkeyClass As Long dwHotKey As Long hIcon As Long hProcess As Long End Type Public Const SEE_MASK_INVOKEIDLIST = &HC Public Const SEE_MASK_NOCLOSEPROCESS = &H40 Public Const SEE_MASK_FLAG_NO_UI = &H400 Public Declare Function ShellExecuteEX Lib "shell32.dll" Alias "ShellExecuteEX" (SEI as SHELLEXECUTEINFO) as Long'定义子程序 Public sub showP(filename$,ownd as Long) dim sei as SHELLEXECUTEINFO dim r as long with sei .cbsize=len(sei) .fmask=&HC or &H40 or &H400 .hwnd=ownd .lpverb="properties" .lpfile=filename .lpparameters=vbnullchar .lpDirectory=vbnullchar .nshow=0 .hinstapp=0 .lpdlist=0 end with r=shellexecuteex(sei) end sub现在你可以在程序中用 showp “c:\command.com",me.hwnd 来条用咯
含目录底下的子目录,求得其所有文件大小之总和
其第二个叁数的目的是起始Static型态的totbyte, 呼叫它时,请不要传叁数进去
Private Sub Command1_Click()
Dim tot As Long
tot = GetDirTotalByte("c:\tools\")
Debug.Print tot
End Sub
Private Function GetDirTotalByte(CurrentPath As String, Optional i As Long) As Long
Static totbyte As Long
Dim nI As Integer, nDirectory As Integer
Dim sFileName As String, sDirectoryList() As String
'Initial totbyte, if it is not the Recursive call the function
If i <> 1 Then
totbyte = 0
End If
'First list all normal files in this directory
sFileName = Dir(CurrentPath, vbNormal + vbHidden + vbReadOnly + vbSystem + vbArchive)
Do While sFileName <> ""
totbyte = totbyte + FileLen(CurrentPath + sFileName)
sFileName = Dir
Loop
'Next build temporary list of subdirectories
sFileName = Dir(CurrentPath, vbDirectory)
Do While sFileName <> ""
'Ignore current and parent directories
If sFileName <> "." And sFileName <> ".." Then
'Ignore nondirectories
If GetAttr(CurrentPath & sFileName) _
And vbDirectory Then
nDirectory = nDirectory + 1
ReDim Preserve sDirectoryList(nDirectory)
sDirectoryList(nDirectory) = CurrentPath & sFileName
End If
End If
sFileName = Dir
Loop
'Recursively process each directory
For nI = 1 To nDirectory
GetDirTotalByte sDirectoryList(nI) & "\", 1
Next nI
GetDirTotalByte = totbyte
End Function
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
' Optional fields
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
Public Const SEE_MASK_INVOKEIDLIST = &HC
Public Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Const SEE_MASK_FLAG_NO_UI = &H400
Public Declare Function ShellExecuteEX Lib "shell32.dll" Alias "ShellExecuteEX"
(SEI as SHELLEXECUTEINFO) as Long'定义子程序
Public sub showP(filename$,ownd as Long)
dim sei as SHELLEXECUTEINFO
dim r as long
with sei
.cbsize=len(sei)
.fmask=&HC or &H40 or &H400
.hwnd=ownd
.lpverb="properties"
.lpfile=filename
.lpparameters=vbnullchar
.lpDirectory=vbnullchar
.nshow=0
.hinstapp=0
.lpdlist=0
end with
r=shellexecuteex(sei)
end sub现在你可以在程序中用 showp “c:\command.com",me.hwnd 来条用咯