Announcement

Collapse
No announcement yet.

GetFileSize

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • GetFileSize

    Can anyone see any obvious problem here?
    It returns zero unless I execute the code at the bottom of the function, but I'd rather use the API call...


    Code:
    Function GetSizeOfFile(FileSpec As String) As Long
    Local l_zFileSpec       As Asciiz * 255
    Local l_hFindFile       As Long
    Local lpFileSizeHigh    As Long
    Local l_Result          As Long
    Local finddata          As WIN32_FIND_DATA
    
    l_zFileSpec = FileSpec
    
    finddata.dwFileAttributes = %FILE_ATTRIBUTE_DIRECTORY
    'Or %FILE_ATTRIBUTE_NORMAL Or %FILE_ATTRIBUTE_ARCHIVE Or %FILE_ATTRIBUTE_READONLY
    l_hFindFile = FindFirstFile(l_zFileSpec, finddata)
    
    If l_hFindFile = %INVALID_HANDLE_VALUE Then
       Function = %INVALID_HANDLE_VALUE  'Check for it when pulling file size from main program otherwise wrong file size!!
       MsgBox "Invalid File Handle",%MB_ICONSTOP,CCS
       Exit Function
    End If
    
    l_Result = GetFileSize(ByVal l_hFindFile, lpFileSizeHigh)
    Function = lpFileSizeHigh
    FindClose l_hFindFile
    
    Exit Function
    'Of course this works:
    l_hFindFile = FreeFile
    Open filespec For Input As #l_hFindFile
    Function = Lof(l_hFindFile)
    Close l_hFindFile
    End Function
    -------------
    Scott
    mailto:[email protected][email protected]</A>
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

  • #2
    Scott --
    I modified you code.
    Code:
       #Compile Exe
       #Register None
       #Dim All
       #Include "WIN32API.INC"
    
       Function GetSizeOfFile(FileSpec As Asciiz) As Double
          Local FindData As WIN32_FIND_DATA, l_hFindFile As Long
          l_hFindFile = FindFirstFile(FileSpec, FindData)
          If l_hFindFile = %INVALID_HANDLE_VALUE Then Function = -1 Else _
             Function = FindData.nFileSizeHigh * &H100000000 + FindData.nFileSizeLow: _
                        FindClose l_hFindFile
       End Function
    
       Function PbMain
          MsgBox Str$(GetSizeOfFile("C:\IO.SYS"))
       End Function
    [This message has been edited by Semen Matusovski (edited February 26, 2000).]

    Comment


    • #3
      I don't understand it, but it works beautiful, thanks!


      Scott

      ------------------
      Scott
      mailto:[email protected][email protected]</A>
      Scott Turchin
      MCSE, MCP+I
      http://www.tngbbs.com
      ----------------------
      True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

      Comment


      • #4
        Scott --
        l_hFindFile = FindFirstFile(FileSpec, FindData) - reads information about file into WIN32_FIND_DATA structure.

        This structure has all necessary parameters, including the file size.
        Because for files more than 4 Mb, one DWORD is not enough,
        a length is stored in two DWORDs (integer value in 8 bytes).
        So, total size is
        nFileSizeHigh * 256 * 256 * 256 * 256 + nFileSizeLow

        [This message has been edited by Semen Matusovski (edited February 26, 2000).]

        Comment


        • #5
          Scott,

          although Semen already gave you a good solution w/ less code, I post this just to show how it is done using GetFileSize.

          Lasse Rantanen
          [email protected]

          Code:
          FUNCTION GetSizeOfFile(FileSpec AS STRING) AS DWORD
              LOCAL l_zFileSpec       AS ASCIIZ * 255
              LOCAL l_hFindFile       AS DWORD
              LOCAL l_dwSize          AS DWORD
              LOCAL l_dwSizeLow       AS DWORD
              LOCAL l_dwSizeHigh      AS DWORD
           
              l_zFileSpec = FileSpec
           
              l_hFindFile = CreateFile(BYVAL VARPTR(l_zFileSpec), _
                                       %GENERIC_READ, _
                                       %FILE_SHARE_READ, _
                                       BYVAL %NULL, _
                                      %OPEN_EXISTING, _
                                      %FILE_FLAG_NO_BUFFERING OR %FILE_FLAG_OVERLAPPED, _
                                      BYVAL %NULL)
           
              IF (l_hFindFile = %INVALID_HANDLE_VALUE) THEN
                  MSGBOX "Failed to open " + FileSpec + $CrLf + $CrLf +_
                         "ERROR " + STR$(GetLastError())
                  FUNCTION = -1
                  EXIT FUNCTION
              END IF
           
              l_dwSizeLow = GetFileSize(l_hFindFile, BYVAL VARPTR(l_dwSizeHigh))
              IF ((l_dwSizeLow = &H0FFFFFFFF) AND (GetLastError() <> %NO_ERROR)) THEN
                  MSGBOX "GetFileSize failed, error " + STR$(GetLastError())
                  FUNCTION = -1
                  EXIT FUNCTION
              END IF
              CloseHandle l_hFindFile
              FUNCTION = MAKDWD(l_dwSizeLow, l_dwSizeHigh)
            
          END FUNCTION

          Comment


          • #6
            Function as DWORD ?
            MAKDWD(l_dwSizeLow, l_dwSizeHigh) ?
            Test your function on big files - 5-6 Gb.
            File size is storing in two DWORD variables (2 * 4 bytes).
            It's Quad without sign.

            [This message has been edited by Semen Matusovski (edited February 26, 2000).]

            Comment


            • #7
              Of course I had to slip an error to my sample. Part of that error is also in Semen's code.

              The return type of the GetSizeOfFile function should be QUAD so that those huge file sizes will be reported correctly.

              And in my sample, line FUNCTION = MAKDWD(... should read e.g.

              Code:
                 Function = l_dwSizeHigh * &H100000000 + l_dwSizeLow
              as in Semen's sample.

              Sorry about this

              Lasse Rantanen
              [email protected]

              Comment


              • #8
                Lasse --
                I declared function as DOUBLE, not as DWORD.

                ------------------

                Comment


                • #9
                  Semen,

                  sorry about that. I also seem to have problems reading carefully. I was narrow mindedly only thinking about integers.

                  Lasse Rantanen
                  [email protected]

                  ------------------

                  Comment


                  • #10
                    Awesome, thank you both

                    The &H10000000 part confused me, think I got it now!


                    Scott

                    ------------------
                    Scott
                    mailto:[email protected][email protected]</A>
                    Scott Turchin
                    MCSE, MCP+I
                    http://www.tngbbs.com
                    ----------------------
                    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

                    Comment

                    Working...
                    X