How to get file size of a given file (in three ways)
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 'File/Folder Handling - How to get file size of a given file (in three ways) 'Returns the size, in bytes, of the specified file Option Explicit 'Using FSO: Public Function GetFileSizeUsingFSO(ByVal sFile As String) As Long 'Set a reference to "Microsoft Scripting Runtime" Dim FSO As Scripting.FileSystemObject Set FSO = New Scripting.FileSystemObject GetFileSizeUsingFSO = FSO.GetFile(sFile).Size Set FSO = Nothing End Function 'How can I call this function: 'Debug.Print GetFileSizeUsingFSO("C:\Temp\b\test1.txt") 'Using FileLen: Public Function GetFileSizeUsingFileLen(ByVal sFile As String) As Long GetFileSizeUsingFileLen = FileLen(sFile) End Function 'How can I call this function: 'Debug.Print GetFileSizeUsingFileLen("C:\Temp\b\test1.txt") 'Using API: Private Declare Function lOpen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, _ ByVal iReadWrite As Long) As Long Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, _ lpFileSizeHigh As Long) As Long Public Function GetFileSizeUsingAPI(ByVal sFile As String) As Long Dim lHandle As Long lHandle = lOpen(sFile, &H0&) GetFileSizeUsingAPI = GetFileSize(lHandle, 0) lclose lHandle End Function 'How can I call this function: 'Debug.Print GetFileSizeUsingAPI("C:\Temp\b\test1.txt") |