Results 1 to 19 of 19

Thread: txt file size?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2004
    Posts
    7

    txt file size?

    halo mornin, may i know how to check a txt file size? thanks

  2. #2

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2004
    Posts
    7

    Re: txt file size?

    Thanks alot

  4. #4

  5. #5
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: txt file size?

    Related question - is there any way to tell how many lines (vbCrLf delimited) there are in a text file? The only way I've done it is to read the file line by line until EOF and use a counter, then I read it again (for example, to put each line in an array). Or, I've dimensioned an array initially to be the size of the file in bytes, then re-size it after I've counted the number of lines and populated the array. Seems to me there is a more elegant method. Collections are OK if the number of lines are small, but for a file with 500k lines, something else is needed. Actually, a stack or queue data structure would be ideal. Any other suggestions?

    VBAhack

  6. #6
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: txt file size?

    VBAhack,

    Much easier method. Read the whole text file into memory (in Binary) in one shot. Split the file on LF's and take the Ubounds of the resulting array. This will give you the total lines in a file quite quickly.

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: txt file size?

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim ff As Integer
    5.   Dim strBuff As String
    6.   Dim str() as string
    7.   ff = FreeFile
    8.   Open "c:\text.txt" For Input As #ff
    9.     strBuff = Input(LOF(ff), ff)
    10.   Close #ff
    11.   str() = split(strbuff, vbcrlf)
    12.   msgbox "There are " & ubound(str) -1 & " sentences in the file"
    13. End Sub

  8. #8
    New Member
    Join Date
    Apr 2005
    Posts
    9

    Re: txt file size?

    Quote Originally Posted by dglienna
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim ff As Integer
    5.   Dim strBuff As String
    6.   Dim str() as string
    7.   ff = FreeFile
    8.   Open "c:\text.txt" For Input As #ff
    9.     strBuff = Input(LOF(ff), ff)
    10.   Close #ff
    11.   str() = split(strbuff, vbcrlf)
    12.   msgbox "There are " & ubound(str) -1 & " sentences in the file"
    13. End Sub
    Hello! Can this code be used to search a text file for a string? When I tried it it worked but not on the whole file. I suppose the file is too big to load in a string. Is there a way around this without going into complex hardcore coding? Sorry but quite limited VB skills

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: txt file size?

    You can use pos = INSTR(strbuff,"Find this")
    if it is found, it will give the offset, if not it will return 0

    how big is your file? I've never had a problem with the size of a string.

    how many lines does it report?

  10. #10
    New Member
    Join Date
    Apr 2005
    Posts
    9

    Re: txt file size?

    6870 lines, a word on each line. I'm trying to search for complete words only not for string within the string (if you know what i mean ) Therefore when I load the file in a string I have to separate the words.
    Something like:
    Line Input #ff, strMystr
    strFull = strFull & Space(1) + strMystr

    Does that make it too big for a string? When I loaded it without spaces it searched the whole file but it's not what I needed.

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: txt file size?

    No, I don't know that there is a limit, but it wouldn't be below 30000 characters

    You might want to load the words into a listbox, which you can then search for whole words, but it seems to me that if you search for a space before and a space after, you would get the whole word.

  12. #12
    New Member
    Join Date
    Apr 2005
    Posts
    9

    Re: txt file size?

    When i load the file into a string it looks like "hgawegudfisduhkljlsiueiyutsahsh..."
    So if I search for 'day' it will say 'Found' because I have the word 'birthday' somewhere in the file. And if I search for ' day ' it won't return any results.

    btw I know nothing about list boxes, should I start learning about them next?

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: txt file size?

    How do you expect to find words if you don't have words to start with. I thought you were splitting them. Show an example of what you have.

  14. #14
    New Member
    Join Date
    Apr 2005
    Posts
    9

    Re: txt file size?

    OK I mean when the string looks like "asdhkasjfjdkfdljgl..." it doesn't work and when I turn it into "dsf dgdfh asda..." it works fine but doesn't load the whole file. I used your code:
    Open "file.txt" For Input As #ff
    Do Until EOF(ff)
    Line Input #ff, strMystr
    strFull = strFull & Space(1) + strMystr
    Loop
    Close #ff
    iPos = InStr(strFull, strWord)
    If iPos > 0 Then
    MsgBox ("Found: ") & strWord
    Else
    MsgBox ("Not Found: ") & strWord
    End If

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: txt file size?

    I must have changed it, but the correct code is what was quoted here:

    http://www.vbforums.com/showpost.php...07&postcount=8

  16. #16
    New Member
    Join Date
    Apr 2005
    Posts
    9

    Re: txt file size?

    I couldn't use Split because I was not sure what it did and what I could achieve with it in my case. What I'm using as it is I found somewhere in your old posts and works well. If only it would work for the whole file I thought about splitting the file into two strings instead. I don't think I know enough to do that though

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: txt file size?

    Here is how split works:

    VB Code:
    1. Dim iNames$
    2. dim str() as string
    3. iNames = "David Greg Vicki Michael Nicole"
    4. if instr(iNames, " ") > 0 then
    5.   str = split(iNames," ") ' you can split on any character(s)
    6. next x
    7. msgbox "The total number of names is: " & ubound(str) + 1

    str(0) = "David"
    str(1) = "Greg"

    and 5 is the total number of names.

  18. #18
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: txt file size?

    Quote Originally Posted by randem
    VBAhack,

    Much easier method. Read the whole text file into memory (in Binary) in one shot. Split the file on LF's and take the Ubounds of the resulting array. This will give you the total lines in a file quite quickly.
    randem, of course! What didn't I think of that! That's what these forums are for, thanks.

    VBAhack

  19. #19
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: txt file size?

    if you want only whole words, use something like :
    instr(text1.text," day ")
    it will search for the spaces too mate

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