Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / September 2003



Tip: Looking for answers? Try searching our database.

Filesize wrongly reported by API call

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dirk - 30 Sep 2003 18:37 GMT
I'm having a problem getting the size of a file.
For a "small" file it all works fine.However for a file of over 2gb it gives
a strange result.

WFD.nFileSizeLow becomes -1347188508

And the file itself has a size of 2,947,778,788

Any ideas what I'm doing wrong?

Below the code I'm using.

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA"
_
                                         (ByVal lpFileName As String, _
                                          ByRef lpFindFileData As
WIN32_FIND_DATA) As Long

Private Const MAX_PATH As Long = 260
Private Const INVALID_HANDLE_VALUE = -1
Private Const MAXDWORD As Long = &HFFFF

Private Type FILETIME
  dwLowDateTime As Long
  dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
  dwFileAttributes As Long
  ftCreationTime As FILETIME
  ftLastAccessTime As FILETIME
  ftLastWriteTime As FILETIME
  nFileSizeHigh As Long
  nFileSizeLow As Long
  dwReserved0 As Long
  dwReserved1 As Long
  cFileName As String * MAX_PATH
  cAlternate As String * 14
End Type

Function GetFileSize(ByVal filePath As String) as double
   Dim WFD As WIN32_FIND_DATA
   Dim hFile As Long
   Dim hTotalSize As Long
   Dim tGetFileSize As Double
   hFile = FindFirstFile(filePath, WFD)
   If hFile <> INVALID_HANDLE_VALUE Then
       tGetFileSize = (WFD.nFileSizeHigh * (maxword + 1)) +
WFD.nFileSizeLow
   Else
       tGetFileSize = -1
   End If
   GetFileSize = tGetFileSize
End Function
Ken Halter - 30 Sep 2003 19:21 GMT
Those are actually the same number.

?hex$(-1347188508) 'gives you...
AFB388E4 'plug this into Windows Calculator and you get....
2,947,778,788

Signature

Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep it in the groups..

> I'm having a problem getting the size of a file.
> For a "small" file it all works fine.However for a file of over 2gb it gives
[quoted text clipped - 3 lines]
>
> And the file itself has a size of 2,947,778,788
Dirk - 30 Sep 2003 19:27 GMT
OK but how to get this into the 2947778788 value?

> Those are actually the same number.
>
[quoted text clipped - 13 lines]
> >
> > And the file itself has a size of 2,947,778,788
Rick Rothstein - 30 Sep 2003 19:41 GMT
This should work...

BigNum = -1347188508
If BigNum < 0 Then
 BigNum = 4294967296 + BigNum
End If

Rick - MVP

> OK but how to get this into the 2947778788 value?
>
[quoted text clipped - 18 lines]
> > >
> > > And the file itself has a size of 2,947,778,788
Ken Halter - 30 Sep 2003 20:16 GMT
Yup... much shorter than mine...

Dim BigNum As Double

Signature

Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep it in the groups..

> This should work...
>
[quoted text clipped - 27 lines]
> > > >
> > > > And the file itself has a size of 2,947,778,788
Rick Rothstein - 30 Sep 2003 20:52 GMT
I wanted to make that If-Then statement a one-liner, but I was afraid of
that news reader line wrapping would have ruined the effect.<g>

Rick - MVP

> Yup... much shorter than mine...
>
[quoted text clipped - 31 lines]
> > > > >
> > > > > And the file itself has a size of 2,947,778,788
Dirk - 30 Sep 2003 20:34 GMT
seems to work that way....

   Dim WFD As WIN32_FIND_DATA
   Dim hFile As Long
   Dim hTotalSize As Long
   Dim tGetFileSize As Currency
   hFile = FindFirstFile(filePath, WFD)
   If hFile <> INVALID_HANDLE_VALUE Then
       tGetFileSize = (WFD.nFileSizeHigh * 4294967296#) +
IIf(WFD.nFileSizeLow < 0, 4294967296# + WFD.nFileSizeLow, WFD.nFileSizeLow)
   Else
       tGetFileSize = -1
   End If
   GetFileSize = tGetFileSize

> This should work...
>
[quoted text clipped - 27 lines]
> > > >
> > > > And the file itself has a size of 2,947,778,788
Ken Halter - 30 Sep 2003 20:09 GMT
Here's one way.... just whipped it up so test with several values...
'============
Option Explicit

Private Function Normalize(SomeVal As Long) As Double
  Dim sHex As String
  If SomeVal < 0 Then
     sHex = Right$("0000000" & Hex$(SomeVal), 8)
     Normalize = Val("&h" & Left$(sHex, 4) & "&") * 65536 _
        + Val("&h" & Mid$(sHex, 5) & "&")
  Else
     Normalize = SomeVal
  End If
End Function

Private Sub Command1_Click()
  Dim d As Double
  d = Normalize(-1347188508)
  Debug.Print d 'shows 2947778788
End Sub
'============

Signature

Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep it in the groups..

> OK but how to get this into the 2947778788 value?
>
[quoted text clipped - 18 lines]
> > >
> > > And the file itself has a size of 2,947,778,788
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.