halo mornin, may i know how to check a txt file size? thanks
Printable View
halo mornin, may i know how to check a txt file size? thanks
VB Code:
MsgBox FileLen("c:\temp\test.txt") & " Bytes" 'or MsgBox FileLen("c:\temp\test.txt") \ 1024 & " KB" 'or ... do your calculations
Thanks alot
:)
No problem. :wave:
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
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.
VB Code:
Option Explicit Private Sub Form_Load() Dim ff As Integer Dim strBuff As String Dim str() as string ff = FreeFile Open "c:\text.txt" For Input As #ff strBuff = Input(LOF(ff), ff) Close #ff str() = split(strbuff, vbcrlf) msgbox "There are " & ubound(str) -1 & " sentences in the file" 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 :DQuote:
Originally Posted by dglienna
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?
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.
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.
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? :bigyello:
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.
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
I must have changed it, but the correct code is what was quoted here:
http://www.vbforums.com/showpost.php...07&postcount=8
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 :(
Here is how split works:
VB Code:
Dim iNames$ dim str() as string iNames = "David Greg Vicki Michael Nicole" if instr(iNames, " ") > 0 then str = split(iNames," ") ' you can split on any character(s) next x msgbox "The total number of names is: " & ubound(str) + 1
str(0) = "David"
str(1) = "Greg"
and 5 is the total number of names.
randem, of course! What didn't I think of that! That's what these forums are for, thanks.Quote:
Originally Posted by randem
VBAhack
if you want only whole words, use something like :
instr(text1.text," day ")
it will search for the spaces too mate :afrog: