Hello,
Please observe the code snippet below:
Private Const OF_READ = &H0&
Private Declare Function lOpen Lib "kernel32" Alias "_lopen" (ByVal
lpPathName As String, ByVal iReadWrite As Long) As Long
Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile
As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long,
lpFileSizeHigh As Long) As Long
Dim lpFSHigh As Long
Public Sub GetInfoF(FilePath As String)
Dim Pointer As Long, sizeofthefile As Long
Pointer = lOpen(FilePath, OF_READ)
'size of the file
sizeofthefile = GetFileSize(Pointer, lpFSHigh)
Label1.Caption = sizeofthefile & " bytes"
lclose Pointer
End Sub
Private Sub command1_Click()
CommonDialog1.ShowOpen
GetInfoF CommonDialog1.filename
End Sub
Private Sub Form_Load()
With CommonDialog1
.DialogTitle = "Select a file"
.Filter = "All the files|*.*"
End With
Command1.Caption = "Select a file"
End Sub
I need to know the size of the file without opening file, because if I open
a large file of more than 100MB lopen may take more time and memory.
Please confirm what I am thinking is right and is there any other alternate
solution to know the size of the file without opening the file.
Thank you,
Regards,
Raj.
Thorsten Albers - 27 Oct 2007 10:49 GMT
Raj <raj@track-mate.net> schrieb im Beitrag
<ORlCLeGGIHA.3600@TK2MSFTNGP06.phx.gbl>...
> Pointer = lOpen(FilePath, OF_READ)
> sizeofthefile = GetFileSize(Pointer, lpFSHigh)
> lclose Pointer
You shouldn't use _lopen() and _lclose() since these Windows API functions
are only provided for compatibility reasons. Use CreateFile() and
CloseHandle() instead.
> I need to know the size of the file without opening file, because if I open
> a large file of more than 100MB lopen may take more time and memory.
Unless you don't want to support files larger then ~2 GB you may use the VB
statements provided for this as well: Open ..., Close ..., FileLen() and
LOF().
> Please confirm what I am thinking is right and is there any other alternate
> solution to know the size of the file without opening the file.
For opening the file with _lopen(), CreateFile(), Open ..., GetFileSize(),
and FileLen() the size of the file doesn't matter. The speed will be the
same for a file of 10 bytes and of 10 gigabytes since the bytes of the file
do >>not<< get counted one by one...

Signature
----------------------------------------------------------------------
THORSTEN ALBERS Universität Freiburg
albers@
uni-freiburg.de
----------------------------------------------------------------------
MikeD - 27 Oct 2007 14:25 GMT
> Hello,
>
[quoted text clipped - 33 lines]
> Please confirm what I am thinking is right and is there any other
> alternate solution to know the size of the file without opening the file.
Don't make things more difficult by using the Win32 API when there's no
reason to. Unless your files are going to be larger than approx 2GB, use
VB's FileLen function, which doesn't require the file be opened.

Signature
Mike
Microsoft MVP Visual Basic
Karl E. Peterson - 29 Oct 2007 21:53 GMT
> I need to know the size of the file without opening file, because if I open
> a large file of more than 100MB lopen may take more time and memory.
>
> Please confirm what I am thinking is right and is there any other alternate
> solution to know the size of the file without opening the file.
I prefer not to open a file, to avoid messing with its datestamp, myself. Use
FindFirstFile if you have reason to suspect some files may be greater than 2Gb.
There are any number of algorithms out there, to correctly interpret the hi/lo parts
of the filesize returned in WIN32_FIND_DATA. For example:
Public Function LargeInteger1(ByVal LoPart As Long, ByVal HiPart As Long) As
Variant
Dim nResult As Variant
Const MaxDWord As Double = 4294967296# '2^32
' Force result to be Decimal type.
nResult = CDec(HiPart)
If HiPart Then
' Correct for sign-bit in high part.
If HiPart < 0 Then
nResult = nResult + MaxDWord
End If
' Shift it up 32-bits.
nResult = nResult * MaxDWord
End If
' Add low part, accounting for sign bit.
If LoPart < 0 Then
nResult = nResult + (LoPart + MaxDWord)
Else
nResult = nResult + LoPart
End If
' Return results.
LargeInteger1 = nResult
End Function
Public Function LargeInteger2(ByVal LoPart As Long, ByVal HiPart As Long) As
Variant
Dim nResult As Currency
' Copy the parts, appropriately.
Call CopyMemory(ByVal VarPtr(nResult), LoPart, 4)
Call CopyMemory(ByVal VarPtr(nResult) + 4, HiPart, 4)
' Return the result as VarType Decimal(14).
LargeInteger2 = nResult * CDec(10000)
End Function

Signature
.NET: It's About Trust!
http://vfred.mvps.org