Hi, how could I get file size from vbcode
any idea
Thank
Printable View
Hi, how could I get file size from vbcode
any idea
Thank
Use the FileLen function.
How would you show the Actual Size On Disk?
FileLen only shows the actual file size?
here's my function, tweak it to your liking
VB Code:
Dim dblFileLen As Double Private Function FileSize(ByVal strFile As String) As String dblFileLen = FileLen(strFile) Select Case dblFileLen Case Is < 1024 FileSize = "< 1 KB" Case Is < 947200 'at ~925KB, show 0.9MB FileSize = dblFileLen \ 1024 & " KB" Case Is < 969932800 'at ~925MB, show 0.9GB FileSize = Round(dblFileLen / 1048576, 1) & " MB" Case Else FileSize = Round(dblFileLen / 1073741824, 2) & " GB" End Select End Function
For the file size:
VB Code:
'*********************** 'API's Public Declare Function GetFileSize Lib "kernel32" _ (ByVal hFile As Long, lpFileSizeHigh As Long) As Long Private Declare Function CreateFile Lib "kernel32" _ Alias "CreateFileA" (ByVal lpFileName As String, _ ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _ ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, _ ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long '*********************** '*********************** 'Constants Const FILE_ATTRIBUTE_ARCHIVE = &H20 Const FILE_ATTRIBUTE_COMPRESSED = &H800 Const FILE_ATTRIBUTE_DIRECTORY = &H10 Const FILE_ATTRIBUTE_HIDDEN = &H2 Const FILE_ATTRIBUTE_NORMAL = &H80 Const FILE_ATTRIBUTE_READONLY = &H1 Const FILE_ATTRIBUTE_SYSTEM = &H4 Const GENERIC_READ = &H80000000 Const OPEN_EXISTING = 3 Const GENERIC_WRITE = &H40000000 '************************ '*********************** 'GetFileSizeBytes '**************** ' ' GetFileSizeBytes: ' Returns the size of a file in bytes '*********************** Function GetFileSizeBytes(FileName As String) As Long 'declarations Dim FileNum As Long 'holds the CreateFile value 'get the file size FileNum = CreateFile(FileName, GENERIC_READ Or GENERIC_WRITE, 0&, 0&, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&) GetFileSizeBytes = GetFileSize(FileNum, 0&) 'close it.... CloseHandle (FileNum) End Function '*********************** 'GetFileSizeKBytes '***************** ' ' GetFileSizeKBytes: ' Returns the size of a file in kilobytes '*********************** Function GetFileSizeKBytes(FileName As String) As Long 'declarations Dim FileNum As Long 'holds the CreateFile value 'get the file size FileNum = CreateFile(FileName, GENERIC_READ Or GENERIC_WRITE, 0&, 0&, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&) GetFileSizeKBytes = (GetFileSize(FileNum, 0&) / 1000) 'close it.... CloseHandle (FileNum) End Function
For the free disk space on a drive:
VB Code:
Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" _ (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, _ lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, _ lpTtoalNumberOfClusters As Long) As Long Dim Sectors As Long Dim Bytes As Long Dim freeClusters As Long, totalClusters As Long Dim retValue As Long Dim freespace As Long retValue = GetDiskFreeSpace("C:\", Sectors, Bytes, freeClusters, totalClusters) freespace = Sectors * Bytes * freeClusters MsgBox freespace
Hope this is what you wanted...
Phreak
Oh, for the above GetDiskFreeSpace example, you should display it in a textbox or label. The return value seems to change depending on wheather it is stored in a variable or in an Object.
Phreak