Get File Size
Skip Navigation Links.

Get File Size

Skip Navigation LinksHome :: Visual Basic :: Get Filesize

Here's a simple Visual Basic function you can use in Vb6+/MS Access to get the size of a file in KiloBytes.

Also requires: Directory related Constants to be declared in the same module and File Errors Handler

Public Function FileSize(ByVal FileName As String) As Long
'Returns the size of a file in KiloBytes
    Dim dl As Long
    Dim Msg As String
    Dim myFileInfo As BY_HANDLE_FILE_INFORMATION
On Local Error GoTo FileSize_Err
'TO DO return Bytes or KB

    If FileName = "" Then
        Exit Function
    Else
        hFile& = CreateFile&(FileName$, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
        
       dl& = GetFileInformationByHandle&(hFile&, myFileInfo)
        With myFileInfo
            FileSize = CStr(.nFileSizeLow) \ 1000
        End With
        
        dl& = CloseHandle&(hFile&)
    End If
    
    If FileSize = 0 Then
        FileSize = 1
    End If

FileSize_End:
   Exit Function

FileSize_Err:
   Resume FileSize_End

End Function

Copy and paste the Visual Basic code below into your module

Please Link to us Next >> Recycle Bin
top of page
all content ©1996/2012 BennySutton.com
all images © their respective owners This site uses Thumbshots previews