Hi all.
In the past I've done most file IO using the old built-in VB statements such as Open, Line Input #, Get, Put and so on. In a recent project I decided to try and update a bit, so I'm using a File object to represent each file, and OpenAsTextStream to create a TextStream object, then reading that using ReadLine.
It's working just fine, except for one thing. How can I tell where I am in the file? I'm dealing with large files (hundreds of MB) so I like to provide a progress indicator, based on the file size and my position in the file. That was perfectly simple before, using LOF and Seek functions. But when using a TextStream in this way, how can I determine my current position? I can get the line number of course, but for that to be any use I need to know the total number of lines.
At the moment I just start out by doing a complete read through the file (using .ReadLine) to count the lines. This is fairly quick (anywhere from 5 to 30 seconds), then I can use that count to provide the progress indicator during the "real" processing (which obviously takes much longer, or I wouldn't bother). But it seems silly to have to do a double pass through the file like this.
17 4678
Hello,
Do you want to add a progressbar or any slider type thing.
Hi,
I had answered this a few days ago.
Use "ReadAll" Method, and Split for vbCrLF, you will get Array's ubound as No. of Lines..: -
Dim FSO As New Scripting.FileSystemObject
-
Dim TArr
-
Dim fsStr As TextStream
-
'
-
Set fsStr = FSO.OpenTextFile("C:\MyText.txt", ForReading)
-
TArr = Split(fsStr.ReadAll, vbCrLf)
-
MsgBox "Total Number Of Lines : " & UBound(TArr)
-
fsStr.Close
-
Set FSO = Nothing
-
Regards
Veena
Use "ReadAll" Method, and Split for vbCrLF, you will get Array's ubound as No. of Lines..:
Thanks for the idea.
I'd rather not, though. I am already counting the number of lines, so I don't need a way to do that (though yours may be faster). Since each file is at least 100-200MB in size, I don't want to be making copies of it all over my RAM. That's why I'm processing it line by line.
Do you want to add a progressbar or any slider type thing.
I already have a progress bar, thanks. What I'm looking for is a better way to determine where I'm up to in the file. In other words, I'm looking for an equivalent of the old Seek() function.
Hi,
FSO dosent have any equivalent of "Seek" operation, you have to settle for IO's Seek. Or write a Seek Function of your own.
Regards
Veena
I already have a progress bar, thanks. What I'm looking for is a better way to determine where I'm up to in the file. In other words, I'm looking for an equivalent of the old Seek() function.
I think this Article page will help you How To Seek Past VBA's 2GB File Limit
Thanks for trying, people, but I guess I'll just have to accept that with the FSO model, MS in their wisdom have thrown out the whole concept of "where I'm up to". No wonder their progress indicators are so inaccurate.
Hari, thanks for the link, but I've already seen that article (some years ago). I'm not trying to go beyond 2GB, just want to know my position within a file. I can't use things like Seek() because I'm not using Open.
Looks as though I only have two real options... - Live with the two-step process I'm using now. That is, first count all the lines, then count them again while processing them to track position, or
- Go back to good old Open and Line Input # processing, and use Seek() function to track progress.
- Just thought of another alternative while I was typing. Since I can easily determine the file size, I can just add up the length of each line as I go, plus 2 for the delimiters, to track my progress. Yippee!
If you don’t mind! Can you post some part of code and let me do some research? (Of-course it will help me to learn.)
Hari, thanks for the link, but I've already seen that article (some years ago). I'm not trying to go beyond 2GB, just want to know my position within a file. I can't use things like Seek() because I'm not using Open.
In text file there is no Row or column ID to indicate where we are. But not willing to say this will b the answer. I want to try more.
It's working just fine, except for one thing. How can I tell where I am in the file? I'm dealing with large files (hundreds of MB) so I like to provide a progress indicator, ........
we cannot show progressbar without knowing Min,CurrentPosition,Max value
Till working on that....
[*]Just thought of another alternative while I was typing. Since I can easily determine the file size, I can just add up the length of each line as I go, plus 2 for the delimiters, to track my progress. Yippee![/list]
YES, Good one, U can get Total Size of the file thru FSO,
and Just get total bytes read till now using the "LenB( )" function,
Convert to kb, and Increment the progress bar accordingly....
Real good option, thanx for that..
Regards
Veena
How about My Program?
I hope this will be usefull!
Hi Hari,
Read Killer's previous posts, he dont want to use "ReadAll"..
Hi Hari,
Read Killer's previous posts, he dont want to use "ReadAll"..
Yha i replied to that post.
In text file there is no Row or column ID to indicate where we are. But not willing to say this will b the answer. I want to try more.
Without row or column id we cannot set directly to any row or column.
But just gussing.....
I'm on a different computer and don't have access to my code. So I'll throw together a demo function to show the sort of process I'm using. First is the FSO version. The second function shows how I would normally do this sort of thing, using Open and Line Input # statements. - Sub ReadFile_FSO_version(FileName As String, PrgBar As ProgressBar)
-
' For simplicity, I'm going to assume the progress bar
-
' has Min = 0, Max = 100.
-
-
Dim fso As New FileSystemObject
-
Dim fil As File
-
Dim str As TextStream
-
Dim TotalLines As Long
-
Dim OnePercent As Long
-
-
-
Set fil = fso.GetFile(FileName)
-
-
' Step 1 - count the lines in the file.
-
Set str = fil.OpenAsTextStream(ForReading)
-
With str
-
Do Until .AtEndOfStream
-
.ReadLine
-
TotalLines = TotalLines + 1
-
Loop
-
.Close
-
End With
-
OnePercent = TotalLines / 100
-
-
' Step 2 - process the data in the file.
-
Set str = fil.OpenAsTextStream(ForReading)
-
With str
-
Do Until .AtEndOfStream
-
.ReadLine
-
' Process the data from this line.
-
-
If .Line Mod 500 = 0 Then
-
PrgBar.Value = .Line / OnePercent
-
DoEvents
-
End If
-
Loop
-
.Close
-
End With
-
Set fil = Nothing
-
-
-
End Sub
-
-
-
Sub ReadFile_NonFSO_version(FileName As String, PrgBar As ProgressBar)
-
' For simplicity, I'm going to assume the progress bar
-
' has Min = 0, Max = 100.
-
-
Dim OnePercent As Long
-
Dim FileSize As Long
-
Dim FileNo As Long, LineNo As Long
-
Dim Text As String
-
-
FileNo = FreeFile
-
Open FileName For Input Access Read Shared As #FileNo
-
FileSize = LOF(FileNo)
-
OnePercent = FileSize / 100
-
-
Do Until EOF(FileNo)
-
Line Input #FileNo, Text
-
LineNo = LineNo + 1
-
-
' Process the data from this line.
-
-
If LineNo Mod 500 = 0 Then
-
PrgBar.Value = Seek(FileNo) / OnePercent
-
DoEvents
-
End If
-
-
Loop
-
-
Close #FileNo
-
-
End Sub
Hi killer42,
Did you saw the zip file in #12, if that is okay then; i am ready to explain!
Did you saw the zip file in #12, if that is okay then; i am ready to explain!
Ok, I've just had a look now.
I see it uses the "ReadAll, Split and Ubound" technique described in this thread. Thanks, it could be a useful technique for getting the count more quickly (I currently read line by line and count them). But it doesn't help in my case, as I want to avoid having to read the file and get the line count.
I think I'll run with the idea that occurred to me while typing my earlier message. That is, get the file size up front by using the old-fashioned FileLen() function, then add up the length of each line as I process them (plus two for the delimiters) to track my position.
Ok, I've just had a look now......
This Seek function will return, how many text a streame read currently So we cannot get the how many line.
We should use progress in timer control ( we can avoid to show each and every line status by changing the timer intervel).
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Kamus of Kadizhar |
last post by:
Thanks to everyone on this list. I now have a functioning piece of
python code!! :-))
Now I'm trying to clean it up.
I have the same (or similar) lines repeated several times:
...
|
by: Zenobia |
last post by:
Hello folks,
Is it possible to position an item within a <td> element? For
instance see below. The table has multiple rows, one for each
database record. Each row has 3 hrefs associated with it...
|
by: GrantS |
last post by:
I am trying to convert the VB.Net code example povided by
http://authors.aspalliance.com/JimRoss/Articles/MaintainScrollPos.aspx
into C# (ASP.Net)without success. No errors are thrown in the VB...
|
by: JE |
last post by:
Hi!
I am snooping around all sorts of websites and faqs, in search of a way to
pick up any selected (mouse-highlighted) text in a HTML page, and store it
in a JavaScript variable.
I have...
|
by: Sunner Sun |
last post by:
Hi, all
Since the OS look both ASCII and binary file as a sequence of bytes, is
there any way to determine the file type except to judge the extension?
Thank you!
|
by: Marc Jennings |
last post by:
Hi there,
Can anyone point out any really obvious flaws in the methodology below
to determine the likely encoding of a file, please? I know the number
of types of encoding is small, but that is...
|
by: blyxx86 |
last post by:
Alright, I'm trying to create a form that would allow certain documents to be opened by selecting an option within a combo box and then having a single button to open the pertaining file.
I know...
|
by: Aussie Rules |
last post by:
Hi,
I want to be able to show a txt file(stored on disk) to the end user in some
sort of txt editor control, and allow the user to highlight/select words
within the text.
Once they have...
|
by: sarika |
last post by:
Hi all
I want to create an online stamp making site. In my site a user select
a template from number of templates for making his stamp. The template
is and image with some graphics and text. Now...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |