|
-
Jun 7th, 2006, 10:01 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Getting correct size of large files
Hello, does anybody know how to get the correct size of files bigger than 2GB?
Files above 2GB give a totally wrong negative value.
I read somewhere that you can use the calculation: ((2^31)*2) + Negative Value
But with a 2.2GB file it seems to be about 3mb off, which is too much as quite a few things in my app rely on the file size.
Any ideas?
-
Jun 7th, 2006, 10:03 AM
#2
Re: Getting correct size of large files
what are u using to get the size?
FileLen()?
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 7th, 2006, 10:08 AM
#3
Thread Starter
Frenzied Member
Re: Getting correct size of large files
Yes. I also tried the GetFileSize API, but that returns the same negative value.
-
Jun 7th, 2006, 10:29 AM
#4
Re: Getting correct size of large files
tried this:
VB Code:
Dim tmp As Double
tmp = FileLen("C:\pagefile.sys")
Debug.Print Format(tmp, "#,##.00") & " Bytes"
tmp = tmp / 1024
Debug.Print Format(tmp, "#,##.00") & " KB"
tmp = tmp / 1024
Debug.Print Format(tmp, "#,##.00") & " MB"
tmp = tmp / 1024
Debug.Print Format(tmp, "#,##.00") & " GB"
and it came out with this
1,195,376,640.00 Bytes
1,167,360.00 KB
1,140.00 MB
1.11 GB
worked fine for this file?
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 7th, 2006, 10:42 AM
#5
Thread Starter
Frenzied Member
Re: Getting correct size of large files
That works fine with files below 2GB, but with files above 2GB it returns a negative value.
The file I tried is 2.148.502,00 KB (shown by windows xp)
This is what it returns:
-2.094.901.248,00 Bytes
-2.045.802,00 KB
-1.997,85 MB
-1,95 GB
-
Jun 7th, 2006, 10:55 AM
#6
Re: Getting correct size of large files
ahh. probably because Filelen returns a long...
and a long limit is -2,147,483,648 to 2,147,483,647
hmmmm
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 7th, 2006, 11:07 AM
#7
Re: Getting correct size of large files
See what happens if you use GetFileSizeEx API.
(I don't have any >2GB file. I can't test it.)
-
Jun 7th, 2006, 11:09 AM
#8
Re: Getting correct size of large files
I stripped the uneccessary code from a bigger class:
Put this code in a class:
VB Code:
Option Explicit
Public Enum enumCreationDisposition
CREATE_NEW = 1
CREATE_ALWAYS = 2
OPEN_EXISTING = 3
OPEN_ALWAYS = 4
TRUNCATE_EXISTING = 5
End Enum
Public Enum enumFileAttribute
FILE_ATTRIBUTE_READONLY = &H1
FILE_ATTRIBUTE_HIDDEN = &H2
FILE_ATTRIBUTE_SYSTEM = &H4
FILE_ATTRIBUTE_DIRECTORY = &H10
FILE_ATTRIBUTE_ARCHIVE = &H20
FILE_ATTRIBUTE_DEVICE = &H40
FILE_ATTRIBUTE_NORMAL = &H80
FILE_ATTRIBUTE_TEMPORARY = &H100
FILE_ATTRIBUTE_SPARSE_FILE = &H200
FILE_ATTRIBUTE_REPARSE_POINT = &H400
FILE_ATTRIBUTE_COMPRESSED = &H800
FILE_ATTRIBUTE_OFFLINE = &H1000
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = &H2000
FILE_ATTRIBUTE_ENCRYPTED = &H4000
End Enum
Public Enum enumShareMode
FILE_SHARE_READ = &H1
FILE_SHARE_WRITE = &H2
FILE_SHARE_READ_WRITE = &H3
FILE_SHARE_DELETE = &H4
End Enum
Public Enum enumDesiredAccess
GENERIC_WRITE = &H40000000
GENERIC_READ = &H80000000
GENERIC_READ_WRITE = &HC0000000
End Enum
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private FHandle As Long
Private FName As String
Public Function OpenFile(Optional ByVal sFileName As String, Optional ByVal Access As enumDesiredAccess = GENERIC_READ_WRITE, _
Optional ByVal ShareMode As enumShareMode = FILE_SHARE_READ_WRITE, _
Optional ByVal CreationDisposition As enumCreationDisposition = OPEN_ALWAYS, _
Optional ByVal Attributes As enumFileAttribute = FILE_ATTRIBUTE_NORMAL) As Boolean
Dim SA As SECURITY_ATTRIBUTES
If sFileName = "" Then sFileName = FName
If FHandle <> 0 Then Me.CloseFile
FHandle = CreateFile(sFileName, Access, ShareMode, SA, CreationDisposition, Attributes, 0)
OpenFile = FHandle <> -1
If Not OpenFile Then
FHandle = 0
Else
FName = sFileName
End If
End Function
Public Function CloseFile() As Boolean
If FHandle <> 0 Then CloseFile = CloseHandle(FHandle) <> 0
If CloseFile Then FHandle = 0
End Function
Public Function FileSize(Optional ByRef SizeHigh As Long = 0) As Long
If FHandle <> 0 Then
FileSize = GetFileSize(FHandle, SizeHigh)
End If
End Function
Public Function FileSizeDouble() As Double
Dim SizeLow As Long, SizeHigh As Long
SizeLow = FileSize(SizeHigh)
FileSizeDouble = CDbl(SizeHigh) * (2 ^ 32) + LongToDouble(SizeLow)
End Function
Private Function LongToDouble(ByVal Lng As Long) As Double
If Lng And &H80000000 = 0 Then
LongToDouble = CDbl(Lng)
Else
LongToDouble = (Lng Xor &H80000000) + (2 ^ 31)
End If
End Function
Private Sub Class_Terminate()
CloseFile
End Sub
To use it:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim A As New clsAPIFile
Dim FSize As Double
If A.OpenFile("C:\Temp\test8.mp3", , , OPEN_EXISTING) Then
FSize = A.FileSizeDouble
Debug.Print FSize
A.CloseFile
End If
End Sub
-
Jun 7th, 2006, 11:59 AM
#9
Thread Starter
Frenzied Member
Re: Getting correct size of large files
Thanks guys, the last two solutions work great
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
|