|
-
Apr 12th, 2005, 08:04 PM
#1
Thread Starter
New Member
txt file size?
halo mornin, may i know how to check a txt file size? thanks
-
Apr 12th, 2005, 08:20 PM
#2
Re: txt file size?
VB Code:
MsgBox FileLen("c:\temp\test.txt") & " Bytes"
'or
MsgBox FileLen("c:\temp\test.txt") \ 1024 & " KB"
'or ... do your calculations
-
Apr 12th, 2005, 08:26 PM
#3
Thread Starter
New Member
Re: txt file size?
Thanks alot
-
Apr 12th, 2005, 08:28 PM
#4
Re: txt file size?
No problem.
-
Apr 13th, 2005, 02:59 PM
#5
Fanatic Member
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
-
Apr 13th, 2005, 03:28 PM
#6
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.
-
Apr 13th, 2005, 03:30 PM
#7
Re: txt file size?
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
-
Apr 13th, 2005, 04:14 PM
#8
New Member
Re: txt file size?
 Originally Posted by dglienna
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
-
Apr 13th, 2005, 04:19 PM
#9
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?
-
Apr 13th, 2005, 05:04 PM
#10
New Member
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.
-
Apr 13th, 2005, 05:24 PM
#11
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.
-
Apr 13th, 2005, 05:48 PM
#12
New Member
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?
-
Apr 13th, 2005, 05:54 PM
#13
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.
-
Apr 13th, 2005, 06:22 PM
#14
New Member
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
-
Apr 13th, 2005, 06:41 PM
#15
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
-
Apr 13th, 2005, 07:10 PM
#16
New Member
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
-
Apr 13th, 2005, 07:54 PM
#17
Re: txt file size?
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.
-
Apr 15th, 2005, 02:57 PM
#18
Fanatic Member
Re: txt file size?
 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
-
Apr 15th, 2005, 04:02 PM
#19
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|