Results 1 to 6 of 6

Thread: File size

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2002
    Location
    CHILE
    Posts
    36

    File size

    Hi, how could I get file size from vbcode

    any idea

    Thank

  2. #2
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Use the FileLen function.
    Please rate my post.

  3. #3
    New Member
    Join Date
    Mar 2004
    Location
    TX
    Posts
    12
    How would you show the Actual Size On Disk?
    FileLen only shows the actual file size?

  4. #4
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048
    here's my function, tweak it to your liking
    VB Code:
    1. Dim dblFileLen As Double
    2.  
    3.  
    4. Private Function FileSize(ByVal strFile As String) As String
    5.  
    6.     dblFileLen = FileLen(strFile)
    7.     Select Case dblFileLen
    8.         Case Is < 1024
    9.             FileSize = "< 1 KB"
    10.         Case Is < 947200 'at ~925KB, show 0.9MB
    11.             FileSize = dblFileLen \ 1024 & " KB"
    12.         Case Is < 969932800 'at ~925MB, show 0.9GB
    13.             FileSize = Round(dblFileLen / 1048576, 1) & " MB"
    14.         Case Else
    15.             FileSize = Round(dblFileLen / 1073741824, 2) & " GB"
    16.     End Select
    17. End Function

  5. #5
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    For the file size:

    VB Code:
    1. '***********************
    2. 'API's
    3. Public Declare Function GetFileSize Lib "kernel32" _
    4. (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
    5. Private Declare Function CreateFile Lib "kernel32" _
    6. Alias "CreateFileA" (ByVal lpFileName As String, _
    7. ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _
    8. ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, _
    9. ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    10. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    11. '***********************
    12.  
    13.  
    14. '***********************
    15. 'Constants
    16. Const FILE_ATTRIBUTE_ARCHIVE = &H20
    17. Const FILE_ATTRIBUTE_COMPRESSED = &H800
    18. Const FILE_ATTRIBUTE_DIRECTORY = &H10
    19. Const FILE_ATTRIBUTE_HIDDEN = &H2
    20. Const FILE_ATTRIBUTE_NORMAL = &H80
    21. Const FILE_ATTRIBUTE_READONLY = &H1
    22. Const FILE_ATTRIBUTE_SYSTEM = &H4
    23. Const GENERIC_READ = &H80000000
    24. Const OPEN_EXISTING = 3
    25. Const GENERIC_WRITE = &H40000000
    26. '************************
    27.  
    28. '***********************
    29. 'GetFileSizeBytes
    30. '****************
    31. '
    32. '   GetFileSizeBytes:
    33. '                     Returns the size of a file in bytes
    34. '***********************
    35.  
    36. Function GetFileSizeBytes(FileName As String) As Long
    37. 'declarations
    38. Dim FileNum As Long                                      'holds the CreateFile value
    39.  
    40. 'get the file size
    41. FileNum = CreateFile(FileName, GENERIC_READ Or GENERIC_WRITE, 0&, 0&, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&)
    42. GetFileSizeBytes = GetFileSize(FileNum, 0&)
    43. 'close it....
    44. CloseHandle (FileNum)
    45. End Function
    46.  
    47.  
    48.  
    49. '***********************
    50. 'GetFileSizeKBytes
    51. '*****************
    52. '
    53. '   GetFileSizeKBytes:
    54. '                      Returns the size of a file in kilobytes
    55. '***********************
    56.  
    57. Function GetFileSizeKBytes(FileName As String) As Long
    58. 'declarations
    59. Dim FileNum As Long                                      'holds the CreateFile value
    60.  
    61. 'get the file size
    62. FileNum = CreateFile(FileName, GENERIC_READ Or GENERIC_WRITE, 0&, 0&, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&)
    63. GetFileSizeKBytes = (GetFileSize(FileNum, 0&) / 1000)
    64. 'close it....
    65. CloseHandle (FileNum)
    66. End Function

    For the free disk space on a drive:

    VB Code:
    1. Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" _
    2.     (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, _
    3.     lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, _
    4.     lpTtoalNumberOfClusters As Long) As Long
    5.  
    6. Dim Sectors As Long
    7. Dim Bytes As Long
    8. Dim freeClusters As Long, totalClusters As Long
    9. Dim retValue As Long
    10. Dim freespace As Long
    11.  
    12. retValue = GetDiskFreeSpace("C:\", Sectors, Bytes, freeClusters, totalClusters)
    13. freespace = Sectors * Bytes * freeClusters
    14.  
    15. MsgBox freespace

    Hope this is what you wanted...


    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Oh, for the above GetDiskFreeSpace example, you should display it in a textbox or label. The return value seems to change depending on wheather it is stored in a variable or in an Object.

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

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