|
-
Mar 2nd, 2004, 06:58 PM
#1
Thread Starter
Member
File size
Hi, how could I get file size from vbcode
any idea
Thank
-
Mar 2nd, 2004, 10:19 PM
#2
Frenzied Member
Use the FileLen function.
-
Mar 3rd, 2004, 12:46 AM
#3
New Member
How would you show the Actual Size On Disk?
FileLen only shows the actual file size?
-
Mar 3rd, 2004, 12:50 AM
#4
Frenzied Member
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
-
Mar 3rd, 2004, 06:24 AM
#5
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
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Mar 3rd, 2004, 06:40 AM
#6
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
Visual Studio 6, Visual Studio.NET 2005, MASM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|