USUÁRIO:      SENHA:        SALVAR LOGIN ?    Adicione o VBWEB na sua lista de favoritos   Fale conosco 

 

  Dicas

  Visual Basic    (Arquivos/Diretórios)

Título da Dica:  Retornando o número de bytes em um diretório
Postada em 31/7/2003 por Danilo Onofre      Clique aqui para enviar email para o autor  danilonofre@ig.com.br
Coloque o conteúdo abaixo num módulo

'API constants
Public Const MAX_PATH = 260
Public Const INVALID_HANDLE_VALUE = -1
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10

'API types
Public Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

Public 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

'API Function calls
Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

'Truncate a String returned by API calls To the first Null char Chr(0)
Private Function APItoString(s As String) As String
    Dim x As Integer

    x = InStr(s, Chr(0))
    If x <> 0 Then
        APItoString = Left(s, x - 1)
    Else
        APItoString = s
    End If
End Function

Public Function DirSpace(sPath As String) As Long
    Dim f As WIN32_FIND_DATA
    Dim hFile As Long
    Dim hSize As Long

    DirSpace = 0
    'Add the slash To the search path
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    'start a file Enum In the specified path
    hFile = FindFirstFile(sPath & "*.*", f)
    If hFile = INVALID_HANDLE_VALUE Then Exit Function
    If (f.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 0 Then
        'Count file size
        DirSpace = DirSpace + f.nFileSizeLow
    ElseIf Left(f.cFileName, 1) <> "." Then
        'Call the DirSpace With subdirectory
        DirSpace = DirSpace + DirSpace(sPath & APItoString(f.cFileName))
    End If
    'Enumerate all the files
    Do While FindNextFile(hFile, f)
        If (f.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 0 Then
            'Count file size
            DirSpace = DirSpace + f.nFileSizeLow
        ElseIf Left(f.cFileName, 1) <> "." Then
            'Call the DirSpace With subdirectory
            DirSpace = DirSpace + DirSpace(sPath & APItoString(f.cFileName))
        End If
    Loop
    'Close the file search
    FindClose (hFile)
End Function


'Usando em qualquer parte da aplicação:
Private Sub Command1_Click()
    Msgbox DirSpace("c:\temp") & " bytes In C:\temp"
End Sub
 


CyberWEB Network Ltda.    © Copyright 2000-2024   -   Todos os direitos reservados.
Powered by HostingZone - A melhor hospedagem para seu site
Topo da página