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?
JDL129Asked:
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.

game-masterCommented:


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.


game-master
0
danaseamanCommented:
Try this:

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

Open in new window

0

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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

HooKooDooKuCommented:
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.
0
danaseamanCommented:
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
Const FILE_TYPE_UNKNOWN = &H0
 
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

0
JDL129Author Commented:
danaseaman:

THANKS YOU!!!!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.