VB searching in text file

  • Thread starter Thread starter AL
  • Start date Start date
A

AL

Hi
I have a bunch of text files which need to be organized.
What I want to do is to select three continuous lines from
each text file and write them down in another text file.
Each of those three line is started with character such
as "IH".
There are also more lines repeated after these three lines
which are started with the same character, but I do not
want to take them.

Thanks
Al
 
Open the file and use the split method to split the file contents at the
return lines (split(mycontents, vbcrlf)). Now check the first member of the
array to make sure it starts with "IH" and take the next two members of the
array. Now write this to a new textstream.

Is this what your after?
(e-mail address removed)
 
Hello,

AL said:
I have a bunch of text files which need to be organized.
What I want to do is to select three continuous lines from
each text file and write them down in another text file.
Each of those three line is started with character such
as "IH".
There are also more lines repeated after these three lines
which are started with the same character, but I do not
want to take them.

You already got an answer to your question. Please always stay in the
original thread. Feel free to reply further questions to your previous
thread.

Regards,
Herfried K. Wagner
 
Hi
How should I use the split function.
Should I just write: Character = lines(Split("IH",
vbCrLf))
I did not find any help in MSDN.
I appreciate if you explain it in detail.
Thanks
 
Hi Al,

In answer to your previous query, Herfried said:

|| Have a look at the classes 'StreamReader' and
|| 'StreamWriter' in the 'System.IO' namespace.
|| You can use the 'StreamReader''s 'ReadLine'
|| method to read a line from the file.

Did you look at them? If not, why not?
Did you thank him? If not, why not?

Method:

Open a StreamWriter for the output file.
For each input file
Open a StreamReader for the input file
Read each line.
If it starts with "IH" then Bingo.
Write the line to the StreamWriter
Read and write the next two lines.
Close the StreamReader
Next
Close the StreamWriter

Regards,
Fergus
 
Hello,

Al said:
How should I use the split function.
Should I just write: Character = lines(Split("IH",
vbCrLf))
I did not find any help in MSDN.
I appreciate if you explain it in detail.

\\\
Dim s As String = "Hello" & ControlChars.NewLine & "Bla"
Dim astr() As String = Split(s, ControlChars.NewLine)
Dim x As String
For Each x In astr
MsgBox(x)
Next x
///

HTH,
Herfried K. Wagner
 
Basic code for reading the lines of a file:

\\\
Imports System.IO
..
..
..
Dim sr As New StreamReader("C:\WINDOWS\WIN.INI")
Dim strLine As String
strLine = sr.ReadLine()
Do Until strLine Is Nothing
MsgBox(strLine)
strLine = sr.ReadLine()
Loop
sr.Close()
///

HTH,
Herfried K. Wagner
 
Hi Fergus,
I did not find those in MSDN help? Are those VB classes?
Let me know where I can find them.
I am going to email Herfried.
Thanks
Ali
 
Hi
I am going to use what you wrote and see what will happen.
Bye the way, regarding to the previous answer you gave me,
I did not find those classes in VB.
Thanks
Al
 
Hi
Bye the way, how should I pick up a line from the file? I
am nearly good in VB, but I still do not know some easy
stuff. sorry
here is what I have.
dim temp as string '( it has been already defined to pick
up a file with a path)
open temp for input as #filenum
?? what shoud I write to get the line in a line character
Once I get the line character I do what you said in
previous reply.

Thanks
Al
 
Al said:
I am going to use what you wrote and see what will happen.
Bye the way, regarding to the previous answer you gave me,
I did not find those classes in VB.

Are you sure, you are talking about VB.NET?
 
Al said:
open temp for input as #filenum

Your code is VB6 (or earlier) code. This is a VB.NET language group (see
group name). For older version please turn to one of the
microsoft.public.vb.* groups.
 
Hi Al,

I have to wonder - are you doing this in VB6 (or earlier) or in VB.NET?
Because StreamReader and StreamWriter are fundamental classes in the .NET
framework.

Herfried gave you a complete example this morning (post timed 11:41am)
which showed you how to use the StreamReader.

However, I accept that if you're still thinking in terms of Open For Input
As #FileNum then this would not mean so much to you.

It's time to say goodbye to Open For... and say hello to Streams.

I've given the complete solution in general terms in my post. Herfried has
given you the details of the reading in his post. The writing is very similar
to the reading.

With what we've given you plus reading and trying out the following, you
should be able to do what you want.

http://support.microsoft.com/default.aspx?scid=kb;en-us;315828#2

Come back with any more queries, but please, it would be nice if you could
show that you've studied what we've given you. :-)

All the best,
Fergus
 
Al said:
I am going to use what you wrote and see what will happen.
Bye the way, regarding to the previous answer you gave me,
I did not find those classes in VB.

Do you use VB.NET or VB Classic?
 
Al said:
I did not find those in MSDN help? Are those VB
classes?

Those are .NET classes (classes included in the .NET Framework).
Let me know where I can find them.
I am going to email Herfried.

Please don't write private mail, always stay in the ngs. Thanks!
 
Al said:
Bye the way, how should I pick up a line from the file? I
am nearly good in VB, but I still do not know some easy
stuff. sorry
here is what I have.

If you use VB Classic (VB6), please turn to one of the microsoft.public.vb.*
ngs. Have a look at the documentation for 'Open', 'Close' and 'Line Input'.
 
Ali said:
Hi
I have VB6. sorry I forgot to mention, because I just
heard about vb.net.
Can I use stream in vb6?

Yes, add a Class file and call it "Stream". Then you've got a Stream class.
Unfortunatelly it doesn't do anything. Hmm...you'd better not use a Stream
in VB6.

SCNR ;-)
 
Back
Top