Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15714

GetFileSize - What is the best code?

$
0
0
Hi,

What is the best code?

Code:

Public Function GetFileSizeEz(ByVal xFile As String) As Currency
    On Error Resume Next
    Dim xFSO As FileSystemObject, xDetails As Scripting.File
    Set xFSO = New FileSystemObject
    Set xDetails = xFSO.GetFile(xFile)
    GetFileSizeEz = xDetails.Size
    Set xDetails = Nothing
    Set xFSO = Nothing
    On Error GoTo 0
End Function

Or

Code:

Option Explicit
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type WIN32_FILE_ATTRIBUTE_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
End Type
Private Declare Function GetFileAttributesExW Lib "kernel32.dll" (ByVal lpFileNamePtr As Long, ByVal fInfoLevelId As Long, ByRef lpFileInformation As WIN32_FILE_ATTRIBUTE_DATA) As Long
Private Declare Sub PutMem4 Lib "msvbvm60.dll" (ByVal Addr As Long, ByVal newValue As Long)
Public Function GetFileSizeEz(ByVal Filename As String) As Currency
    Dim I As Long, T As Currency, xAttr As WIN32_FILE_ATTRIBUTE_DATA
    I = GetFileAttributesExW(UniCodePtr(Filename), 0, xAttr)
    If I <> 0 Then
        If Not (xAttr.dwFileAttributes And vbDirectory) <> 0 Then
            PutMem4 ((VarPtr(T) Xor &H80000000) + 4&) Xor &H80000000, xAttr.nFileSizeHigh
            PutMem4 VarPtr(T), xAttr.nFileSizeLow
            GetFileSizeEz = T * 10000
        End If
    End If
End Function
Public Function UniCodePtr(ByVal Filename As String) As Long
    Dim xUniCodePtr As String
    If Not LenB(Filename) = 0 Then
        If AscW(Filename) = 92 Then
            If AscW(Mid$(Filename, 2, 1)) = 92 Then
                xUniCodePtr = "\\?\UNC\" & Right$(Filename, Len(Filename) - 2)
            Else
                xUniCodePtr = "\\?\" & Left$(CurDir, 2) & Filename
            End If
        Else
            xUniCodePtr = "\\?\" & Filename
        End If
        UniCodePtr = StrPtr(xUniCodePtr)
    End If
End Function

Regards!

Viewing all articles
Browse latest Browse all 15714

Trending Articles