Results 1 to 13 of 13

Thread: GetFileSize - What is the best code?

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    44

    GetFileSize - What is the best code?

    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!

  2. #2
    Frenzied Member
    Join Date
    Dec 2014
    Posts
    1,442

    Re: GetFileSize - What is the best code?

    theres many ways, the easiest is:

    Code:
    FileLen()
    another way is using API

    Code:
    Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
    if you want to use scripting

    Code:
    Public Function GetFileSizeEz(ByVal xFile As String) As Currency
    Dim oFSO
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    GetFileSizeEz = oFSO.GetFile(xFile).Size
    Set oFSO = Nothing
    End Function
    and many many many other ways....
    but the 2nd one is a bit over the top

  3. #3
    Taking a Break
    Join Date
    Jan 2020
    Posts
    1,340

    Re: GetFileSize - What is the best code?

    Code:
    F = FreeFile(0)
    Open File1 For Binary Access Read As #F
    Remaining = LOF(F)
    Code:
    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)
    sizeofthefile = GetFileSize(Pointer, lpFSHigh)
    msgbox sizeofthefile & "个字节"
    lclose Pointer
    End Sub
    '-------------------
    Code:
    Private Declare Function GetFileSize Lib "kernel32" Alias "GetCompressedFileSizeA" (ByVal lpFileName As String, lpFileSizeHigh As Long) As Long
    Private Sub Command1_Click()
    Dim a
    a = GetFileSize("c:\1.zip", ByVal 0&)
    Last edited by xiaoyao; Apr 28th, 2021 at 12:59 PM.

  4. #4
    Taking a Break
    Join Date
    Jan 2020
    Posts
    1,340

    Re: GetFileSize - What is the best code?

    Code:
    function GetSysFileSizeEx(AbsFileName:string):int64;
     
    	var
     
    	Handle: Windows.THandle;
     
    	FindData: Windows.TWin32FindData;
     
    	begin
     
    	Handle := Windows.FindFirstFile(PChar(AbsFileName), FindData);
     
    	if Handle <> INVALID_HANDLE_VALUE then
     
    	begin
     
    	Windows.FindClose(Handle);
     
    	if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then //文件
     
    	begin
     
    	Result := INT64(FindData.nFileSizeHigh shl 32)+INT64(FindData.nFileSizeLow);
     
    	Exit;
     
    	end;
     
    	end;
     
    	Result := 0;
     
    	end;
    Code:
    	function GetFileSize(const fName: AnsiString): Int64;
     
    	var
     
    	  hFile: THandle;
     
    	begin
     
    	  hFile := _lopen(PAnsiChar(fName), OF_READ);
     
    	  Result := FileSeek(hFile, Int64(0), 2);
     
    	  _lclose(hFile);
     
    	end;

  5. #5
    Taking a Break
    Join Date
    Jan 2020
    Posts
    1,340

    Re: GetFileSize - What is the best code?

    There is no fastest way to get the space of a folder.Read a drive letter size of the footprint, the need for these two functions, the faster the better.

  6. #6
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    318

    Re: GetFileSize - What is the best code?

    RC5/RC6 (vbRichClient):

    Code:
    '--- GetFileSize ---
    New_c.FSO(FileName).FileLen
    
    
    '--- GetTotalFileSizInDir ---
    New_c.FSO(DirPath).TotalFileSizeInDir

  7. #7
    Taking a Break
    Join Date
    Jan 2020
    Posts
    1,340

    Re: GetFileSize - What is the best code?

    Quote Originally Posted by SearchingDataOnly View Post
    RC5/RC6 (vbRichClient):

    Code:
    '--- GetFileSize ---
    New_c.FSO(FileName).FileLen
    
    
    '--- GetTotalFileSizInDir ---
    New_c.FSO(DirPath).TotalFileSizeInDir
    You have integrated so many functions into one DLL, which is equivalent to the VB6. Net framework

  8. #8
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,784

    Re: GetFileSize - What is the best code?

    @baka: FileLen returns the size as a long, therefore will not work for any file larger than 2GB (2,147,483,647 bytes, 2^31 - 1)

    GetFileSize calls that don't combine the 2 values have the same issue.

    Here's a version with GetFileSizeEx that supports Unicode and files over 2GB:
    Code:
    Public Declare Function GetFileSizeEx Lib "kernel32" (ByVal hFile As Long, lpFileSize As Currency) As Boolean
    Public Declare Function CreateFileW Lib "kernel32" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    
    Public Const GENERIC_READ    As Long = &H80000000
    Public Const FILE_SHARE_READ = &H1&
    Public Const OPEN_EXISTING = 3&
    
    Public Function GetFileSizeW(sFile As String) As Currency
    Dim hFile As Long, nSize As Currency
    hFile = CreateFileW(StrPtr(sFile), GENERIC_READ, FILE_SHARE_READ, ByVal 0&, OPEN_EXISTING, ByVal 0&, ByVal 0&)
    Call GetFileSizeEx(hFile, nSize)
    GetFileSizeW = nSize * 10000
    Call CloseHandle(hFile)
    End Function
    But another question, do you want to display the formatted value, e.g. '123 bytes' or '2,345 KB'... or what's appropriate for whatever system you're on, e.g. if French Windows is installed, '123 octets' or '2,345 Ko'?

    Then you can use the Property System:
    Use the method found in this project, and ask for "System.Size" or PKEY_Size

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,330

    Re: GetFileSize - What is the best code?

    Just use the functions built into VB6. That handles all but a vanishingly small number of cases.

  10. #10

  11. #11
    Taking a Break
    Join Date
    Jan 2020
    Posts
    1,340

    Re: GetFileSize - What is the best code?

    for test :
    get all file size on driver c:
    which method is quickly

    4 types of tests:
    1. Get the size of a single file quickly

    2. Get all files in the directory (time used)
    3. Get the space occupied by the folder
    4. Get the space occupied by a certain disk and the remaining space

    If there are 8000 files in the C:\windows\system32 directory, save them as a list a.txt, and then obtain the file size of these 8000 files, so as to be more accurate. Maybe some methods are faster for small files, but slower for large files.
    Last edited by xiaoyao; Apr 29th, 2021 at 01:22 AM.

  12. #12
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,512

    Re: GetFileSize - What is the best code?

    Quote Originally Posted by xiaoyao View Post
    for test :
    get all file size on driver c:
    which method is quickly
    Errr?
    DiskCapacity - FreeSpaceOnDisk?
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  13. #13
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,027

    Re: GetFileSize - What is the best code?

    Quote Originally Posted by hennyere View Post
    . . .
    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!
    Just don't *ever* use this UniCodePtr function implementation. This is a disaster waiting to happen.

    cheers,
    </wqw>

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