Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get the size of a file in VB6?

Status
Not open for further replies.

SkennyR

Programmer
Mar 7, 2004
157
US
I get an overflow error when the file Im writing to reaches approx 2.8 megs.
How can I manage this?
I need to be able to know the file size in my program, and alert the user when the file size reaches maximum.
Appreciate the help, tried searching for it on this forum, didnt yield any positive results.
Here is an abstract example of what Im doing:
Open "filename" for append as#1
write#1,a,b,c
close 1
If the file gets to a certain size, when I try to read from in, it gives me a data overflow error.
Im reading (simply) like this:
open "filename" for input as #1
do until eof(1)
input#1, a,b,c
loop
close.
 
There is no implicit overflow at 2.8Mb built into VB - I'm sat here happily writing 30Mb files...

Another way of getting the file length (though the previous poster's solution is perhaps better - this is just for your information) is FileLen()

mmilan
 
Use either the File System Object to read the file or the API. VB's LOF and FileLen do not seem to handle files of large sizes.

Swi
 
Thanks to all.
After I think for a minute, Im not sure why Im getting the data overflow error.
Very first thing I do upon start of the program, is get the last line in the file, and by doing so, display the last line of info, and count all the lines.
Here is a sample of what Im doing:

open "filename" for input as #1
do until eof(1)
x=x+1
input#1, a,b,c,d
loop
labelx.caption=a 'this should be last entry in file
labely.caption=b ' " "
labelz.caption=c ' ""
label#.caption=x ' this should be total number of entries
text1.text=a ' this should be last entry in file

This gives me an data overflow error if the file has over
32766 lines. Under that amount does fine. Do I need to clear up the strings after this operation? I would think that each string would be overwritten on each loop? Im thinking maybe new memory is getting used on each loop?
Thanks for any help.
 
Hello again...
I think I found my problem.
After looking into the possibilty of the memory not getting freed up on each loop, I think I found the problem I was having.
On thissection of code:

do until eof(1)
x=x+1
input#1, a,b,c,d
loop

I was counting x each time. I found that I had previously diminished x as an integer.
Dim x as int
I changed it to
Dim x as long
This fixed my problem.
X was going over its limit, thereby giving me the data overflow error.

But, thanks to the suggestions you guys gave me, can I shorten the code considerably by using one of the commands you suggested to give me the number of lines in the file?
Thanks again, this forum is great.
Id be lost in the tall weeds without it.
 
SkennyR,

This function will show the number of lines in the file:

Private Function ShowNumberOfLines(ByVal pstrFileName As String) As Long

Dim objFso As New FileSystemObject
Dim objPCLITxtStrm As TextStream
Dim lngNumberOfLines As Long

Set objPCLITxtStrm = objFso_OpenTextFile(pstrFileName)

lngNumberOfLines = 0
While Not objPCLITxtStrm.AtEndOfStream
objPCLITxtStrm.ReadLine
lngNumberOfLines = lngNumberOfLines + 1
Wend

objPCLITxtStrm.Close
ShowNumberOfLines = lngNumberOfLines

Set objPCLITxtStrm = Nothing
Set objFso = Nothing

End Function

vladk
 
Thanks!
I will try that.
Appreciate the help!
 
Or, just for fun:

Code:
[COLOR=blue]Public Function GetLineCount(strFile As String) As Long
    On Error Resume Next
        GetLineCount = UBound(Split(CreateObject("scripting.filesystemobject").opentextfile(strFile).readall, vbLf))
    On Error GoTo 0
End Function
[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top