Results 1 to 9 of 9

Thread: [RESOLVED] Getting correct size of large files

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Resolved [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?

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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"

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Getting correct size of large files

    Yes. I also tried the GetFileSize API, but that returns the same negative value.

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Getting correct size of large files

    tried this:
    VB Code:
    1. Dim tmp As Double
    2.     tmp = FileLen("C:\pagefile.sys")
    3.     Debug.Print Format(tmp, "#,##.00") & " Bytes"
    4.     tmp = tmp / 1024
    5.     Debug.Print Format(tmp, "#,##.00") & " KB"
    6.     tmp = tmp / 1024
    7.     Debug.Print Format(tmp, "#,##.00") & " MB"
    8.     tmp = tmp / 1024
    9.     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"

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    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

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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"

  7. #7
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    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.)
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  8. #8
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Getting correct size of large files

    I stripped the uneccessary code from a bigger class:

    Put this code in a class:
    VB Code:
    1. Option Explicit
    2.  
    3. Public Enum enumCreationDisposition
    4.     CREATE_NEW = 1
    5.     CREATE_ALWAYS = 2
    6.     OPEN_EXISTING = 3
    7.     OPEN_ALWAYS = 4
    8.     TRUNCATE_EXISTING = 5
    9. End Enum
    10.  
    11. Public Enum enumFileAttribute
    12.     FILE_ATTRIBUTE_READONLY = &H1
    13.     FILE_ATTRIBUTE_HIDDEN = &H2
    14.     FILE_ATTRIBUTE_SYSTEM = &H4
    15.     FILE_ATTRIBUTE_DIRECTORY = &H10
    16.     FILE_ATTRIBUTE_ARCHIVE = &H20
    17.     FILE_ATTRIBUTE_DEVICE = &H40
    18.     FILE_ATTRIBUTE_NORMAL = &H80
    19.     FILE_ATTRIBUTE_TEMPORARY = &H100
    20.     FILE_ATTRIBUTE_SPARSE_FILE = &H200
    21.     FILE_ATTRIBUTE_REPARSE_POINT = &H400
    22.     FILE_ATTRIBUTE_COMPRESSED = &H800
    23.     FILE_ATTRIBUTE_OFFLINE = &H1000
    24.     FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = &H2000
    25.     FILE_ATTRIBUTE_ENCRYPTED = &H4000
    26. End Enum
    27.  
    28. Public Enum enumShareMode
    29.     FILE_SHARE_READ = &H1
    30.     FILE_SHARE_WRITE = &H2
    31.     FILE_SHARE_READ_WRITE = &H3
    32.     FILE_SHARE_DELETE = &H4
    33. End Enum
    34.  
    35. Public Enum enumDesiredAccess
    36.     GENERIC_WRITE = &H40000000
    37.     GENERIC_READ = &H80000000
    38.     GENERIC_READ_WRITE = &HC0000000
    39. End Enum
    40.  
    41. Private Type SECURITY_ATTRIBUTES
    42.     nLength As Long
    43.     lpSecurityDescriptor As Long
    44.     bInheritHandle As Long
    45. End Type
    46.  
    47. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    48. 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
    49. Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
    50.  
    51. Private FHandle As Long
    52. Private FName As String
    53.  
    54. Public Function OpenFile(Optional ByVal sFileName As String, Optional ByVal Access As enumDesiredAccess = GENERIC_READ_WRITE, _
    55.         Optional ByVal ShareMode As enumShareMode = FILE_SHARE_READ_WRITE, _
    56.         Optional ByVal CreationDisposition As enumCreationDisposition = OPEN_ALWAYS, _
    57.         Optional ByVal Attributes As enumFileAttribute = FILE_ATTRIBUTE_NORMAL) As Boolean
    58.     Dim SA As SECURITY_ATTRIBUTES
    59.    
    60.     If sFileName = "" Then sFileName = FName
    61.     If FHandle <> 0 Then Me.CloseFile
    62.     FHandle = CreateFile(sFileName, Access, ShareMode, SA, CreationDisposition, Attributes, 0)
    63.    
    64.     OpenFile = FHandle <> -1
    65.    
    66.     If Not OpenFile Then
    67.         FHandle = 0
    68.     Else
    69.         FName = sFileName
    70.     End If
    71. End Function
    72.  
    73. Public Function CloseFile() As Boolean
    74.     If FHandle <> 0 Then CloseFile = CloseHandle(FHandle) <> 0
    75.     If CloseFile Then FHandle = 0
    76. End Function
    77.  
    78. Public Function FileSize(Optional ByRef SizeHigh As Long = 0) As Long
    79.     If FHandle <> 0 Then
    80.         FileSize = GetFileSize(FHandle, SizeHigh)
    81.     End If
    82. End Function
    83.  
    84. Public Function FileSizeDouble() As Double
    85.     Dim SizeLow As Long, SizeHigh As Long
    86.     SizeLow = FileSize(SizeHigh)
    87.    
    88.     FileSizeDouble = CDbl(SizeHigh) * (2 ^ 32) + LongToDouble(SizeLow)
    89. End Function
    90.  
    91. Private Function LongToDouble(ByVal Lng As Long) As Double
    92.     If Lng And &H80000000 = 0 Then
    93.         LongToDouble = CDbl(Lng)
    94.     Else
    95.         LongToDouble = (Lng Xor &H80000000) + (2 ^ 31)
    96.     End If
    97. End Function
    98.  
    99. Private Sub Class_Terminate()
    100.     CloseFile
    101. End Sub
    To use it:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim A As New clsAPIFile
    5.     Dim FSize As Double
    6.    
    7.     If A.OpenFile("C:\Temp\test8.mp3", , , OPEN_EXISTING) Then
    8.        
    9.         FSize = A.FileSizeDouble
    10.        
    11.         Debug.Print FSize
    12.        
    13.         A.CloseFile
    14.     End If
    15. End Sub

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    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
  •  



Click Here to Expand Forum to Full Width