Results 1 to 5 of 5

Thread: Quickest way to find out if txt file is empty

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344
    What would be quickest way?
    -RaY
    VB .Net 2010 (Ultimate)

  2. #2
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Probably the GetFileSize API would be fastest. http://www.vbapi.com/ref/g/getfilesize.html

    Or you could check the LOF of the file, but that would require opening the file in VB.

    Code:
    Private Sub Command1_Click()
        Dim strFileName As String
        Dim intFileNum As Integer
        
        intFileNum = FreeFile
        strFileName = "c:\query.log"
        
        Open strFileName For Input As #intFileNum
        If LOF(intFileNum) = 0 Then
            MsgBox "File is empty"
        End If
        Close #intFileNum
        
    End Sub
    [Edited by jbart on 10-13-2000 at 09:55 AM]

  3. #3
    Guest
    I assume that on each OS you will have the same (albeit slightly different from other OS's) file size for the same number of characters.

    If you are using only one OS, simply find the filesize for an empty file and compare it to the current file size of the target file.

    Alternatively, you could write a dummy blank file, get THAT file size and compare it to the filesize of the target file.

    I don't know, however, that that would be faster (in real time) than just opening up the file and attempting to read the first characters.

    Good Luck
    DerFarm

    Well, I see that Jbart beat me to it, and his answer is better anyway......

  4. #4
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Or use
    Code:
    Private Function IsEmpty(File As String) As Boolean
    If FileLen(File) = 0 Then
        IsEmpty = True
    Else
        IsEmpty = False
    End If
    End Function
    
    'USAGE:
    'MsgBox IsEmpty("C:\myfile.txt")
    I think it's a bit slower than the api, but it makes no noticable difference I think!


    [Edited by Jop on 10-13-2000 at 10:08 AM]
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  5. #5
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Thanks Jop. I never used FileLen before. Looks like that would be the quickest and easiest way, since you do not need to open the file.

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