how do I find a file size with VB6 code?

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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.

Try this:

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

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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.
API is faster than FSO. Here is sample:

Option Explicit
Const GENERIC_READ = &H80000000
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

JDL129Author Commented:

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.