how do I find a file size with VB6 code?

Posted on 2008-11-18
Last Modified: 2012-05-05
I am trying to open a file for input to a varible in VB6.  I only want to attempt to open the file if it contains text.  How do I determine that the size of the file is zero bytes so I can skip or delete that file?
Question by:JDL129
    LVL 3

    Expert Comment

    LVL 13

    Expert Comment


    sub checkFileSize()

    Dim fso As New FileSystemObject
    Dim objFile As File

    Set objFile = fso.GetFile(<the file paht>)  'ex. "c"\test.txt"

    If objFile.Size = 0 Then
        objFile.Delete True
        MsgBox "delete"

    End If

    end sub

    i hope this could help you...or can give u idea.

    LVL 22

    Accepted Solution

    Try this:

    Dim FileSize As Long
    FileSize& = FileLen("C:\SOMEFILE.TXT")
    msgbox filesize& & " bytes"

    Open in new window

    LVL 16

    Expert Comment

    If you are dealing with a large number of files, you don't want to use FileLen.  Assuming it's still coded the way it was back in VB5, FileLen is EXTREAMLY inefficient because under the hood, it still finds the file size by opening the file and reading through it.  I've been told this is a hold-over from the days when file size couldn't be determined by looking at the FAT directory inforamtion, because it used to only record the number of sectors the file occupied, no the number of bytes within that sector.  

    Our solution was to call API functions from VB, but that was before the days of the FileSystemObject.
    LVL 22

    Expert Comment

    API is faster than FSO. Here is sample:

    Option Explicit
    Const GENERIC_READ = &H80000000
    Const FILE_SHARE_READ = &H1
    Const OPEN_EXISTING = 3
    Const FILE_TYPE_CHAR = &H2
    Const FILE_TYPE_DISK = &H1
    Const FILE_TYPE_PIPE = &H3
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    Private Declare Function GetFileSizeEx Lib "kernel32" (ByVal hFile As Long, lpFileSize As Currency) As Boolean
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Sub Form_Load()
       Debug.Print FileSize("c:\autoexec.bat")
    End Sub
    Private Function FileSize(ByVal strFilePath As String) As Currency
        Dim hFile As Long, nSize As Currency, sSave As String
        hFile = CreateFile(strFilePath, GENERIC_READ, FILE_SHARE_READ, ByVal 0&, OPEN_EXISTING, ByVal 0&, ByVal 0&)
        GetFileSizeEx hFile, nSize
        CloseHandle hFile
        FileSize = nSize * 10000
    End Function

    Open in new window


    Author Closing Comment


    THANKS YOU!!!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Suggested Solutions

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now