Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


how do I find a file size with VB6 code?

Posted on 2008-11-18
Medium Priority
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 13

Expert Comment

ID: 22990755

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

danaseaman earned 200 total points
ID: 22990832
Try this:

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

Open in new window

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 16

Expert Comment

ID: 22995372
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

ID: 22995538
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


Author Closing Comment

ID: 31518086


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

569 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