Opening up in RichTextBox2 ?

K

Keith (Southend)G

I'm trying to get the resulting text to open up in RichTextBox2, the
first bit works fine but the second bit 'I.O.StreamWriter' I can't
seem to find the correct code.
I can get the "Text Written to File" window pop up, but not with the
code in the second set below. I have manually created at "sorted.txt"
file which I would expect to appear in RichTextBox2, but it doesn't.
Any help would be great.

Thanks

Keith (Southend)

1st set

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "C:\ogimet.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)

RichTextBox1.Text = objReader.ReadToEnd

objReader.Close()
Else
MsgBox("File Does Not Exist")
End If
End Sub


2nd Set


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim FILE_NAME As String = "C:\sorted.txt"

If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)

RichTextBox2.Text = objWriter.Write

objWriter.Close()
MsgBox("Text written to file")
Else
MsgBox("File Does Not Exist")
End If
End Sub
 
K

Keith (Southend)G

The Write method of the StreamWriter class doesn't return a value; in VB
terms, it's implemented as a Sub. There wouldn't be any data for it to
assign to the Text property of the RichTextBox control. Also, I can't find
an overload of the method that accepts no arguments, so I'm not sure how
your objWriter.Write call isn't generating a compile error.

Normally, what I'd do would be to build a string of data that was going to
the file, write it through the StreamWriter instance, and separately copyit
to the display control (the RichTextBox, in this case).

Thanks Jack.

I'm probably not approaching this this best way, but hoping to reach a
position where I get the file(s) read and written to where I want them
and then I can start 'playing with the parsing bit, which is the bit
that does all the tricks. I've moved on a little and only get one
error message, although pressing F5 it runs. Mike previously gave me a
start with some of that basic code, but I suspect the 'observation'
bit I need to rename or rename something else...

Warning 1 Variable 'observation' is used before it has been assigned a
value. A null reference exception could result at runtime. C:
\Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects
\Open Text\Open Text\Form1.vb 46 39 Open Text

The code now looks like this:
It is probably obvious once you know, but what happens is I click on
button 1 "Get Text" and in the RichTextBox1 opens up my 'ogimet.txt'
file with all the numbers in it. Now when I click on "Sort Text" no
errors come up but in RixhTextBox2 there is nothing, which is probably
because my parsing as it stands leaves nothing, but when I look at my
original "ogimet.txt" file it is now empty 0 KB. What's causing this?
I know you mentioned 'overload' with 'objWriter.Write', not sure what
this is. An example of what 'ogimet.txt' looks like can be found
here....
http://www.southendweather.net/ogimet.txt
(This file I update every 12 hours or so and the creation of this will
be all part of this project, however, one thing at a time).

Many thanks. Keith

<snip>
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "C:\ogimet.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)

RichTextBox1.Text = objReader.ReadToEnd

objReader.Close()
Else
MsgBox("File Does Not Exist")
End If
End Sub

and...

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim FILE_NAME As String = "C:\ogimet.txt"
Dim observation As String
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)


Using fs As New System.IO.FileStream _
("C:\sorted.txt", System.IO.FileMode.Open)


Dim sr As New System.IO.StreamReader(fs)
Dim input As String = sr.ReadLine()
While (input <> Nothing)


If (Not input.StartsWith("#")) Then
observation = observation & " " & input
End If


If (observation.EndsWith("=")) Then
' parse the line here...


' set the current observation to blank to
start a new run
observation = ""
End If


input = sr.ReadLine()
End While
End Using
objWriter.Close()
MsgBox("Text written to file")
Else
MsgBox("File Does Not Exist")
End If
End Sub
End Class
<snip>
 
K

Keith (Southend)G

"Keith (Southend)G" <[email protected]> wrote in message
Okay, all that out of the way... I'm not completely sure what you're trying
to accomplish overall here. It kinda looks like you want to read some data
from a text file, find lines that match a pattern, parse some data out of
those lines, and write the data back. Am I on track here? If you describe
the steps of the problem you're trying to solve (without regard to how it
would be done in VB) I'd be happy to try to suggest some VB code for doing
that.

Yes, your description just about sums up what I'm trying to do.
Ok Jack, I'll try to explain what I'm trying to achieve, but I imagine
it will be more involved, I maybe asking to much of you. Even so, as
there is so much that I'm sure I could do I am keen to pick VB up and
develop things further, just need to get a head start, see some
examples etc. Basically, the idea is to save a lot of time I spend
doing things manually. It's a hobby I've had since I was 17, I'm now
knocking on 50 :)

There are thousands of locations that provide synop code (weather),
but for this example I will use 2 locations, the first is the nearest
to where I live and the other is just randomly chosen from Spain.

What I want to do is have a separate file for each time.
1. The date/time '200912301350' etc can be ignored.
2. The date/time of importance is the AAXX 30144 etc. (AAXX means land
station, 30 is date (30th) 14 is time (14:00), the last '4' can be
ignored, this can sometimes be a 0, 1, 3 or 4, it tells you whether
windspeed is reported in mph, mps or knots. So AAXX 30094 is 30th
09:00 hours etc.
3. The numbers 03693 or 08360 is the wmo (World Meteorological
Organisations) station number, this number I would want at the start
of each line.
4. The '=' signifies the end of each line of code.
5. The resulting file(s) / file names I want to then look like, see
bottom of this post...

################################################################################
# SYNOPS from 03693, Shoeburyness (United Kingdom) | 51-33N | 000-50E
| 2 m
################################################################################
200912301350 AAXX 30144 03693 46244 /0412 10046 20046 39920 49922
53001 91350
333 88/04=
200912301250 AAXX 30134 03693 46250 /0512 10048 20048 39914 49917
50001 91250
333 88/04=
200912301150 AAXX 30124 03693 16250 /0411 10049 20048 39916 49919
50007 69921 91150
333 88/05=
200912301050 AAXX 30114 03693 46259 /0612 10051 20049 39918 49920
51016 91050
333 88/05=
200912300950 AAXX 30104 03693 46259 /0512 10050 20049 39914 49916
53014 90950
333 88/05=
200912300850 AAXX 30094 03693 46262 /0511 10051 20051 39910 49912
53013 90850
333 88/05=

################################################################################
# SYNOPS from 08360, Alicante / El Altet (Spain) | 38-17N | 000-33W |
43 m
################################################################################
200912301400 AAXX 30144 08360 46/// /1704 10230 20138 30016 40051
56029
555 60005=
200912301300 AAXX 30134 08360 46/// /1907 10213 20131 30026 40062
58021
555 60005=
200912301200 AAXX 30124 08360 02681 32305 10222 20110 30032 40068
58014 60001 82202
333 60007=
200912301100 AAXX 30114 08360 46/// /2206 10207 20122 30045 40081
50005
555 60005=
200912301000 AAXX 30104 08360 46/// /2003 10191 20121 30047 40083
51010
555 60005=
200912300900 AAXX 30094 08360 22681 30000 10150 20116 30046 40083
53014 82801
333 60007=

5. continued... Resulting files to look like this..... and saved into
a specified directory, can be C:/**********.
The 2009 (year) and 12 (month) could be taken from the original text
or from the computers clock.

File name say: 2009123009.txt
03693 46262 /0511 10051 20051 39910 49912 53013 90850 333 88/05=
08360 22681 30000 10150 20116 30046 40083 53014 82801 333 60007=

2009123010.txt
03693 46259 /0512 10050 20049 39914 49916 53014 90950 333 88/05=
08360 46/// /2003 10191 20121 30047 40083 51010 555 60005=

2009123011.txt
03693 46259 /0612 10051 20049 39918 49920 51016 91050 333 88/05=
08360 46/// /2206 10207 20122 30045 40081 50005 555 60005=

2009123012.txt
03693 16250 /0411 10049 20048 39916 49919 50007 69921 91150 333 88/05=
08360 02681 32305 10222 20110 30032 40068 58014 60001 82202 333 60007=

2009123013.txt
03693 46250 /0512 10048 20048 39914 49917 50001 91250 333 88/04=
08360 46/// /1907 10213 20131 30026 40062 58021 555 60005=

2009123014.txt
03693 46244 /0412 10046 20046 39920 49922 53001 91350 333 88/04=
08360 46/// /1704 10230 20138 30016 40051 56029 555 60005=

To give you an idea of how many locations there are look here:
http://www.southendweather.net/2009123018.txt
This is a file from 18:00z this evening.

Many thanks
Keith (Southend)
email: (e-mail address removed)
 
K

Keith (Southend)G

Dim observation As String = ""

Which initializes the variable to a value at the same time as it's declared.

Second:

Your original file is being erased because you're opening a StreamWriter on
it in the Button2 event handler, here:

Dim objWriter As New System.IO.StreamWriter(FILE_NAME)

Because FILE_NAME was set to the the same file name that you were originally
reading a few lines above that. The StreamWriter is emptying out the file in
preparation for sending data to it. Since there's no code (in this sample,
anyway) that's sending any data down the StreamWriter, the file ends up
empty.


Ah ah, I've got it to create the new file 'sorted.txt' without
overwriting the 'ogimet.txt' file by declaring it as FILE_NAME2 as
follows:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim observation As String = ""
Dim FILE_NAME As String = "C:\ogimet.txt"
Dim FILE_NAME2 As String = "C:\sorted.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME2)

It may seem a little, but I do feel I have made some progress in
understanding this.

I won't give up :)

Keith (Southend)
 
K

Keith (Southend)G

One thing that I'm sure is quite straight forward, when you know how,
that I wonder how you would write it?

1. The following works fine but need a little refinement. What code
would you need to get it to roll through 2 or 3 lines, including the
data, until it gets to the '='. Currently the result in the following
example is:

30124 88963 31360 72510 10012 21026 30006 40022 58001 70272 85670

when I would like to have it like this (All on one line, no wrap):

30124 88963 31360 72510 10012 21026 30006 40022 58001 70272 85670 333
10014 21035 41000 56560 82708 85710 86360 90961 92417 93100 95000=

eg:
Base data...
200912301200 AAXX 30124 88963 31360 72510 10012 21026 30006 40022
58001 70272 85670
333 10014 21035 41000 56560 82708 85710 86360
90961 92417 93100 95000=


While input IsNot Nothing
' Assuming input is "200912301350 AAXX [...] 333
88/04="
Dim ItemList() As String = Split(input, " ")
If ItemList.Length > 2 AndAlso ItemList(1) =
"AAXX" Then
Dim Year As Integer = CInt(ItemList
(0).Substring(0, 4))
Dim Month As Integer = CInt(ItemList
(0).Substring(4, 2))
Dim Day As Integer = CInt(ItemList(2).Substring
(0, 2))
Dim Hours As Integer = CInt(ItemList
(2).Substring(2, 2))
Dim LineDate As Date = DateSerial(Year, Month,
Day).AddHours(Hours)
Dim NewLine As String = ItemList(2)
For Counter As Integer = 3 To ItemList.Length
- 1
NewLine &= " " & ItemList(Counter)
Next
objWriter.WriteLine(NewLine)
End If

Thanks

Keith (Southend)
 
K

Keith (Southend)G

"Keith (Southend)G" <[email protected]> wrote in message


Hi Jack,

A lot of errors here, looks like I need to be declaring a few things
or something to do with Local / Private. Sorry to have to post the
whole code, but you may see where it's going wrong. I tried a few
things, like removing the 'Private', which cut down the errors, but
I'm sure this does something that's required.


Error 1 to 4 'Private' is not valid on a local constant declaration. C:
\Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects
\Open Text\Open Text\Form1.vb 31 9 Open Text
Error 5 Statement cannot appear within a method body. End of method
assumed. C:\Documents and Settings\Keith\My Documents\Visual Studio
2008\Projects\Open Text\Open Text\Form1.vb 36 5 Open Text
Error 6 Name 'InputFile' is not declared. C:\Documents and Settings
\Keith\My Documents\Visual Studio 2008\Projects\Open Text\Open Text
\Form1.vb 37 50 Open Text
Error 7 Expression expected. C:\Documents and Settings\Keith\My
Documents\Visual Studio 2008\Projects\Open Text\Open Text\Form1.vb 40
51 Open Text
Error 8 Property access must assign to the property or use its value.
C:\Documents and Settings\Keith\My Documents\Visual Studio
2008\Projects\Open Text\Open Text\Form1.vb 41 1 Open Text
Error 9 End of statement expected. C:\Documents and Settings\Keith\My
Documents\Visual Studio 2008\Projects\Open Text\Open Text\Form1.vb 41
27 Open Text
Error 10 Method arguments must be enclosed in parentheses. C:
\Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects
\Open Text\Open Text\Form1.vb 41 27 Open Text
Error 11 & 12 Name 'Writer' is not declared. C:\Documents and Settings
\Keith\My Documents\Visual Studio 2008\Projects\Open Text\Open Text
\Form1.vb 48 21 Open Text
Error 13 Declaration expected. C:\Documents and Settings\Keith\My
Documents\Visual Studio 2008\Projects\Open Text\Open Text\Form1.vb 102
21 Open Text
Error 14 & 15 'End While' must be preceded by a matching 'While'. C:
\Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects
\Open Text\Open Text\Form1.vb 103 17 Open Text
Error 16,17,19 Declaration expected. C:\Documents and Settings\Keith
\My Documents\Visual Studio 2008\Projects\Open Text\Open Text\Form1.vb
105 13 Open Text
Error 18 Statement cannot appear outside of a method body. C:
\Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects
\Open Text\Open Text\Form1.vb 107 9 Open Text
Error 20 'End If' must be preceded by a matching 'If'. C:\Documents
and Settings\Keith\My Documents\Visual Studio 2008\Projects\Open Text
\Open Text\Form1.vb 109 9 Open Text
Error 21 'End Sub' must be preceded by a matching 'Sub'. C:\Documents
and Settings\Keith\My Documents\Visual Studio 2008\Projects\Open Text
\Open Text\Form1.vb 110 5 Open Text




Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "C:\ogimet.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)

RichTextBox1.Text = objReader.ReadToEnd

objReader.Close()
Else
MsgBox("File Does Not Exist")
End If
End Sub


Private Sub TabPage1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)

End Sub

Private Sub RichTextBox1_TextChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
RichTextBox1.TextChanged

End Sub

Private Sub RichTextBox2_TextChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
RichTextBox2.TextChanged

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click

Private Const InputFolder As String = "C:\"
Private Const InputFile As String = InputFolder & "ogimet.txt"
Private Const OutputFolder As String = "C:\"
Private Const OutputFile As String = OutputFolder &
"sorted.txt"

Private Sub ProcessInput()
Dim Reader As New System.IO.StreamReader(InputFile, _

System.Text.Encoding.ASCII)
Dim Writer As New System.IO.StreamWriter(OutputFile, _
False,
System.Text.Encoding.ASCII)
Dim InputLine As String = ""
Do
InputLine = GetNextLine(Reader)
If InputLine > "" Then
Dim OutputLine As String = ParseInput(InputLine)
If OutputLine > "" Then
Writer.WriteLine(OutputLine)
End If
End If
Loop Until InputLine = ""
Writer.Close()
Reader.Close()
End Sub

Private Function GetNextLine(ByVal FromReader As
System.IO.StreamReader) _
As String
Dim Result As String = ""
Dim InsideDataLine As Boolean = False
Dim Buffer As String = ""
Do
Buffer = FromReader.ReadLine
If Buffer IsNot Nothing Then
If Not InsideDataLine Then
If Buffer.Length >= 4 AndAlso _
Buffer.Substring(0, 4) Like "2[0-9][0-9][0-9]"
Then
Result = Buffer.Trim
If Result.EndsWith("=") Then
Exit Do
End If
InsideDataLine = True
End If
Else
Result &= " " & Buffer.Trim
If Buffer.EndsWith("=") Then
Exit Do
End If
End If
End If
Loop Until Buffer Is Nothing
Return Result
End Function

Private Function ParseInput(ByVal InputString As String) As String
Dim ItemList() As String = Split(InputString, " ")
Dim NewLine As String = ""
If ItemList.Length > 2 AndAlso ItemList(1) = "AAXX" Then
Dim Year As Integer = CInt(ItemList(0).Substring(0, 4))
Dim Month As Integer = CInt(ItemList(0).Substring(4, 2))
Dim Day As Integer = CInt(ItemList(2).Substring(0, 2))
Dim Hours As Integer = CInt(ItemList(2).Substring(2, 2))
Dim LineDate As Date = DateSerial(Year, Month,
Day).AddHours(Hours)
NewLine = ItemList(2)
For Counter As Integer = 3 To ItemList.Length - 1
NewLine &= " " & ItemList(Counter)
Next
End If
Return NewLine
End Function


input = sr.ReadLine()
End While
End Using
objWriter.Close()
MsgBox("Text written to file")
Else
MsgBox("File Does Not Exist")
End If
End Sub
End Class


This has been very useful as it has made me realize how much there is
to this and that I need to understand precisely what each line is
doing, which you explained well in your previous post. It's a shame I
couldn't unroll on a class locally, but you only get very basic
computer courses, not programming and the like. It's frustrating as I
could save myself so much time and anguish, the trouble is the weather
data never stops, so I'm chasing rainbows, which uses the time I need
to learn this. However, I'm not giving up yet ;-)

Many thanks

Keith (Southend)
 
K

Keith (Southend)G

Sure thing. :)

Looks like it should be easy to fix. Everything that's inside the sub
Button2_Click needs to go outside of it. Some of the declarations (like
Private Const) can't be inside the sub definition, they have to be in the
class definition. If you move all of that code out (say, put it after the
End Sub) then Button2_Click just needs to look like this:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
    ProcessInput()
End Sub

What will happen then is that, when Button2 is clicked, th ProcessInput sub
will be run, which will in turn run the other subs. But all of the subs,
functions, and variables declared with Private scope have to be outside of
the sub; that way, VB knows where to find them. Right now, VB is seeing
those things inside of the sub, and it just doesn't like them to be there..

Something needs to go between End Sub and Private Const?
Class definition? That's right at the top. Down to 15 errors now.

Keith (Southend)


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
ProcessInput()
End Sub

Private Const InputFolder As String = "C:\"
Private Const InputFile As String = InputFolder & "ogimet.txt"
Private Const OutputFolder As String = "C:\"
Private Const OutputFile As String = OutputFolder & "sorted.txt"

Private Sub ProcessInput()
 
K

Keith (Southend)G

Something needs to go between End Sub and Private Const?
Class definition? That's right at the top. Down to 15 errors now.

Keith (Southend)

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
        ProcessInput()
    End Sub

    Private Const InputFolder As String = "C:\"
    Private Const InputFile As String = InputFolder & "ogimet.txt"
    Private Const OutputFolder As String = "C:\"
    Private Const OutputFile As String = OutputFolder & "sorted.txt"

    Private Sub ProcessInput()


The problems seem to be within here, I have put a cross where error
flag up...

Private Sub ProcessInput()
Dim Reader As New System.IO.StreamReader(InputFile, _

System.Text.Encoding.ASCII)
Dim Writer As New System.IO.StreamWriter(OutputFile, _
,X False,
all textX System.Text.Encoding.ASCII)
Dim InputLine As String = ""
Do
InputLine = GetNextLine(Reader)
If InputLine > "" Then
Dim OutputLine As String = ParseInput(InputLine)
If OutputLine > "" Then
WriterX Writer.WriteLine(OutputLine)
End If
End If
Loop Until InputLine = ""
WriterX Writer.Close()
Reader.Close()
End Sub

I must be trying your patience :-(

Keith (Southend)
 
K

Keith (Southend)G

No, no, you're fine... I'm having trouble seeing where we're at, at this
point, I'm afraid. Can you repost the whole class, the way you have it now
(without the additional notations)? I'll import it into Visual Studio on my
end, then I can see the actual error messages being generated. If I can see
any changes that need to be made, I'll edit them in, and send the whole
class back.


Thanks Jack,

This is how it currently looks from top to tail...

Keith (Southend)

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "C:\ogimet.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)

RichTextBox1.Text = objReader.ReadToEnd

objReader.Close()
Else
MsgBox("File Does Not Exist")
End If
End Sub


Private Sub TabPage1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)

End Sub

Private Sub RichTextBox1_TextChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
RichTextBox1.TextChanged

End Sub

Private Sub RichTextBox2_TextChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
RichTextBox2.TextChanged

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
ProcessInput()
End Sub

Private Const InputFolder As String = "C:\"
Private Const InputFile As String = InputFolder & "ogimet.txt"
Private Const OutputFolder As String = "C:\"
Private Const OutputFile As String = OutputFolder & "sorted.txt"

Private Sub ProcessInput()
Dim Reader As New System.IO.StreamReader(InputFile, _

System.Text.Encoding.ASCII)
Dim Writer As New System.IO.StreamWriter(OutputFile, _
False,
System.Text.Encoding.ASCII)
Dim InputLine As String = ""
Do
InputLine = GetNextLine(Reader)
If InputLine > "" Then
Dim OutputLine As String = ParseInput(InputLine)
If OutputLine > "" Then
Writer.WriteLine(OutputLine)
End If
End If
Loop Until InputLine = ""
Writer.Close()
Reader.Close()
End Sub

Private Function GetNextLine(ByVal FromReader As
System.IO.StreamReader) _
As String
Dim Result As String = ""
Dim InsideDataLine As Boolean = False
Dim Buffer As String = ""
Do
Buffer = FromReader.ReadLine
If Buffer IsNot Nothing Then
If Not InsideDataLine Then
If Buffer.Length >= 4 AndAlso _
Buffer.Substring(0, 4) Like "2[0-9][0-9][0-9]"
Then
Result = Buffer.Trim
If Result.EndsWith("=") Then
Exit Do
End If
InsideDataLine = True
End If
Else
Result &= " " & Buffer.Trim
If Buffer.EndsWith("=") Then
Exit Do
End If
End If
End If
Loop Until Buffer Is Nothing
Return Result
End Function

Private Function ParseInput(ByVal InputString As String) As String
Dim ItemList() As String = Split(InputString, " ")
Dim NewLine As String = ""
If ItemList.Length > 2 AndAlso ItemList(1) = "AAXX" Then
Dim Year As Integer = CInt(ItemList(0).Substring(0, 4))
Dim Month As Integer = CInt(ItemList(0).Substring(4, 2))
Dim Day As Integer = CInt(ItemList(2).Substring(0, 2))
Dim Hours As Integer = CInt(ItemList(2).Substring(2, 2))
Dim LineDate As Date = DateSerial(Year, Month,
Day).AddHours(Hours)
NewLine = ItemList(2)
For Counter As Integer = 3 To ItemList.Length - 1
NewLine &= " " & ItemList(Counter)
Next
End If
Return NewLine
End Function


input = sr.ReadLine()
End While
End Using
objWriter.Close()
MsgBox("Text written to file")
Else
MsgBox("File Does Not Exist")
End If
End Sub
End Class
 
K

Keith (Southend)G

Ah, I see... I was getting confused because of the way the text gets wrapped
in the messages here. The problem is there are nine lines at the end that
don't belong there, like a fragment of code that didn't get deleted.

I commented out those lines at the end, and the program ran just fine; that
is, clicking on Button2 created the file sorted.txt from the file
ogimet.txt, without any errors.

Just so I don't mess it up, I saved the code out to a text file, and put it
up where you can download it, if you want. The extra lines are still in the
file, I just marked them out as comments so they don't issue an error. I
also deleted a few blank lines, but that didn't make any difference, I just
did that in passing out of habit. You can get this latest version of the
code here:

http://www.CaptainJack3d.com/Code/ogprog.txt

Let me know if you have any trouble with that.

Hello Jack,

Success :)

The only thing I had to change was take out the '\tmp' from "C:\Tmp\"
to get it work. I've noticed some other little errors, but I need to
have a closer look to see whats causing it, maybe an error in the
original code.

Big 50 this weekend !

Many thanks

Keith (Southend)
 
K

Keith (Southend)G

Excellent. :-D

I forgot about the Tmp... I put my test files in a sub-folder. Glad you
found it.

Congrats, and have a good 'un.

Hi Jack,

I've had a good check though and there are no errors, everything has
'sorted' correctly :)
I'm getting quite upbeat about this now, I can see the light at the
end of the tunnel, not to mention an extra 1/2 hour lay in every
morning ;-)

Now, for me to be able to use this 'additional' synop code, with what
I have, I need to then sort into individual date/time files. I guess
this step would read the 'new' 'sorted.txt' file and look at the first
group of numbers to 'trim'.

eg. taking a small sample from our 'sorted.txt' file, a separate file
for 14154, 14164, 14174, 14184 etc, would need to be created, although
ignore the 5th digit '4' in this example, as this number varies (/,
0,1, 3, 4.), again saved in" C\:"

"sorted.txt"
14184 03693 16349 /1102 10031 20031 30081 40083 53028 60042 91750 333
10035 87/06 88/22=
14174 03693 46350 /1204 10031 20031 30068 40071 53021 91650 333 87/08
86/21=
14164 03693 46350 /1105 10033 20033 30058 40061 52017 91550 333 82/08
88/21=
14154 03693 46356 /1304 10034 20034 30053 40055 53014 91450 333 85/08
87/24=
14184 03707 17/02 /1207 10017 20017 30088 40095 51029 60002 733//
91750 333 10027=
14174 03707 47/03 /1006 10018 20018 30080 40087 52032 734// 91650=
14164 03707 47013 /1105 10019 20019 30069 40076 53024 720// 91550 333
88/01=
14154 03707 47105 /1306 10023 20023 30058 40066 53016 732// 91450 333
88/02=

So the result would look something like this:
File name: "C\:1418.txt"
14184 03693 16349 /1102 10031 20031 30081 40083 53028 60042 91750 333
10035 87/06 88/22=
14184 03707 17/02 /1207 10017 20017 30088 40095 51029 60002 733//
91750 333 10027=

"C\:1417.txt"
14174 03693 46350 /1204 10031 20031 30068 40071 53021 91650 333 87/08
86/21=
14174 03707 47/03 /1006 10018 20018 30080 40087 52032 734// 91650=

"C\:1416.txt"
14164 03693 46350 /1105 10033 20033 30058 40061 52017 91550 333 82/08
88/21=
14164 03707 47013 /1105 10019 20019 30069 40076 53024 720// 91550 333
88/01=

"C\:1415.txt"
14154 03693 46356 /1304 10034 20034 30053 40055 53014 91450 333 85/08
87/24=
14154 03707 47105 /1306 10023 20023 30058 40066 53016 732// 91450 333
88/02=


I think I'm beginning to understand how this all hangs together now,
the important thing is to take one step at a time, you have to have
quite a clear and methodical mind. Therefore, if I'm correct what I'm
saying, the second procedure would be new code that would follow on
from we already have? So I would need to start with something like:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
ProcessInput()
End Sub

' following

Return NewLine
End Function

Private Const InputFolder As String = "C:\"
Private Const InputFile As String = InputFolder & "sorted.txt"
Private Const OutputFolder As String = "C:\"
' This is the point where the individual files are created...
Private Const OutputFile As String = OutputFolder & "******"

I imagine all this can still come within "Sub Button2_Click" ?

I hope I'm not being a bit cheeky here ;-)

Many thanks

Keith (Southend)
 
K

Keith (Southend)G

That's about the most accurate description of a "programmer" I've ever
heard. :-D

You flatter me :)
Nope, I'm enjoying this. :)

Wow, I wasn't expected such a quick reply :)

Oh, I don't feel quite so bad now, I was worrying "You were giving an
inch and I was taking a yard". You don't know how much time all this
will save me, I can grow old more slowly. There is also the data
collection from the 'ogimet' website I haven't mentioned yet, where I
have created a form, but not got any further, now that one really will
be a challenge :-0
Weather has been a hobby since I was 15 and the internet has opened up
so many things.

OK, that once again worked straight away, but as usual my spec. was
flawed ;-)

Nothing major, one minor thing and two nicety's.

1. In the new files, now that we have them all separated I don't need
the 1st date/time numbers.
before:
14154 88963 42260 80518 11010 21023 39986 40002 57039=
14154 88968 41458 81410 10022 21011 39984 49999 58004 71022=
After:
88963 42260 80518 11010 21023 39986 40002 57039=
88968 41458 81410 10022 21011 39984 49999 58004 71022=

2., More of a nicety than a necessity but is it possible to then have
the final data in numerical order, so the 1st one would be
'01001' (happens to be Jan Mayen(9m)-Norway) and the last probably
'98851'(Gen.Santos(15m, Philippines)

3. Again a nicety, some locations report 'nil', so basically there's
no data, eg.
14151 14447 NIL=
In these instances nothing needs to be outputted, they can be ignored.


Many many thanks

Keith (Southend)
 
K

Keith (Southend)G

(1) and (3) I think will be pretty easy, but I'm not sure what you mean for
number 2. Do you want the lines in each file sorted by the numbers in the
line (but each line left intact) or do you mean you want to individual
numbers in each line to be sorted left to right, so that the output lines
look different than they do in the input file?

I've uploaded an example of what I'm looking for:
Before:
http://www.southendweather.net/1418.txt
After:
http://www.southendweather.net/1418b.txt

(I used excel, imported the 1418.txt file, deliminated it with put one
vertical line after the first date/time numbers, then sorted the
second column numerically). I use this a lot, so you can understand
why I'm pulling my hair out <grin>

Basically, the first series of 5 numbers (time/date) are removed and
then the rest are sorted numerically in order by what was the the
second series of 5 numbers, but will now be the 1st. (Clear as mud
isn't it!)

Keith (Southend)
 
K

Keith (Southend)G

Gotcha. I tried a couple of small changes to the code, but we really need
something a little more robust. Ideally, what we need to do is to read in
the data, and build a list of output file names, then, within each file
name, build (then sort) the data that will go to each file. My first thought
was to stick in a SortedList object, but it doesn't like duplicate keys.
What I'm going to do is to make some classes that will do all of the code
processing. That's better procedure anyway, that way the file processing
work is separated from the form controls, and it'll be easier to make a nice
user interface later, if you want.

I'll have some time this weekend to bang out some code, and get it set up
for you to look at. I'll be out of the office on Monday (and thus, away from
my newsreader), but I'll have you something to play with by Tuesday.
Assuming you survive whatever celebrations you're planning for the weekend,
of course.  :)

Much appreciated Jack, don't lose any sleep over it.
Have a nice weekend, I have a big party tomorrow, will be great :)

Keith (Southend)
 
K

Keith (Southend)G

Hope all went well for you... I had a head cold most of the weekend, but my
team won their divisional playoff game, so all was not lost. :)

Jack

Had a great weekend thanks Jack, I hope you're feeling better now.

Ok thanks for the code, I substituted it for the old code and hit F5.
My Form comes up, but when I click on "Get Text" - Button1 it just
hangs with the hour glass, if then click anywhere else it say's "Form
1 (Not responding)

If I click on 'Sort Text' it pops up with this line highlighted in
yellow:
Reader As New System.IO.StreamReader(InputFile,
System.Text.Encoding.ASCII)
and a box next it saying along the lines: 'NonSupportedExeption was
unhandled' ~ The given path's format is not supported, followed by
some trouble shooting tips.
No 'Errors/Warnings/Messages' in the Error list.

Thanks

Keith (Southend)
 
K

Keith (Southend)G

Had a great weekend thanks Jack, I hope you're feeling better now.

Ok thanks for the code, I substituted it for the old code and hit F5.
My Form comes up, but when I click on "Get Text" - Button1 it just
hangs with the hour glass, if then click anywhere else it say's "Form
1 (Not responding)

If I click on 'Sort Text' it pops up with this line highlighted in
yellow:
Reader As New System.IO.StreamReader(InputFile,
System.Text.Encoding.ASCII)
and a box next it saying along the lines: 'NonSupportedExeption was
unhandled' ~ The given path's format is not supported, followed by
some trouble shooting tips.
No  'Errors/Warnings/Messages' in the Error list.

Thanks

Keith (Southend)

I should have said 'Sort Text' is my Button 2.
Also, this was when I stopped debugging, then again pressed F5 but
went straight to Button 2. It maybe coming up as Button 1 gets the
"ogimet.txt" files, so by only pressing Button 2 there is no data
perhaps?

I did try adding the full path here:
' Name of the input file (including the input folder)
Private Const InputFile As String = InputFolder & "ogimet.txt" ~
"C:/ogimet.txt"

but then it went straight to that other message.

Keith (Southend)
 
K

Keith (Southend)G

I should have said 'Sort Text' is my Button 2.
Also, this was when I stopped debugging, then again pressed F5 but
went straight to Button 2. It maybe coming up as Button 1 gets the
"ogimet.txt" files, so by only pressing Button 2 there is no data
perhaps?

I did try adding the full path here:
' Name of the input file (including the input folder)
    Private Const InputFile As String = InputFolder & "ogimet.txt"  ~
"C:/ogimet.txt"

but then it went straight to that other message.

Keith (Southend)

Just looked at Windows Task Manager while it's 'Running' Open
Text.vshost.exe CPU 99, Mem usage 30,000 to 39,000k. Not sure if this
has any significance? (I have 1Gig RAM)
No text appears in textbox1.

Tell a lie, it does, but it took about 5 minutes. Also the 2nd bit
worked really quickly.
Yes, the output looks perfect and sorted in numerical order.

This is the business Jack :)

Keith (Southend)
 
K

Keith (Southend)G

Good, and I'm glad you had a good weekend. I'm feeling much better today,
thanks, but I wish I could have had a weekend that wasn't all bleary from
cold medicine. :)

The error you referenced before would happen if there was something wrong
with the file name; if it's getting that error, it probably can't find the
file.

I may have just jumped ahead to the next step before it had 'read/
uploaded' to textbox1, thinking it was done. I'm wondering whether it
has something to do with opening in 'textbox1' as a larger file was no
problem before, when we didn't. My original thinking with the
textbox's in the form probably isn't actually required, as long as it
parses the file 'ogimet.txt' it's those output files that are
important to me.
When you push either button the first time, it's going to read the data in;
nothing will go into the text display until it's down. I'm not sure why it
would take five minutes unless you've downloaded a lot more data than I've
got here (I've got about 1400 lines in my test file, and it loads in about
two seconds on my computer. When you ckick the second button after having
clicked the first, the data's already loaded, so it would proceed directly
to making the output.

Yes the file is bigger, I uploaded the latest one:
http://www.southendweather.net/ogimet.txt
This is actually my next problem, I have to download each Countries
data one at a time, first setting the dates/time etc, see:
http://www1.ogimet.com/synopsc.phtml.en
I pick 38 different Countries, which takes me about 10 minutes.
The size of the file depends on what time span I set, basically I'm
grabbing it continuously and depending on what time I do it determines
how many hours worth I need to catch up.
The data class could be modified to raise an event as it's reading the file
(I think; I don't know that I've ever raised events in a class constructor
before, but it would be easy to adjust if need be). The form could be
modified to read the event and update the text display as each line is read,
so you'd have a better idea of what's happening.

Would this speed that bit up, I timed it the next time and the latest
file took 5 minutes and 20 seconds before it appeared in 'textbox1'?
Again, this wasn't a problem before.

I guess these things are trial and error, but 'when there's a will
there's a way', as they say ;-)

Out of interest where are you based?

Many thanks

Keith (Southend)
 
K

Keith (Southend)G

I don't know what criteria you're using for selecting your data, but that
can possibly be automated as well. The WebBrowser control can go to a web
page and retrieve it, then it gives you access to the page contents
(including form fields) and lets you fill in the forms programmatically.
It's a little tricky to use, though, and if they every change the web page
on you, you often have to change your program later.


Actually no; in fact, it would slow it down slightly. The difference would
be that you could see it doing something instead of getting all your results
at the end. Sometimes that makes things seem faster, is all. :)


I'm in Indianapolis, Indiana, a little east of the middle of the USA.

Jack,

Firstly, thank you for spending so much time on that code, especially
explaining what it's doing, step by step. It's quite daunting at
first, but I guess after a while you get used to certain common code
techniques. The help you're giving me frees up more time to study it
more closely, I guess I'm in a bit of a catch 22 with time.

Ok, I've double checked the output of all the files last night and
everything looks good, this is a great time saver for me. Is there
anyway round the 5 minute wait when first clicking Button1 to populate
RichTextBox1? This I guess will only get longer the larger the file,
although at the most the file size would be double the one I uploaded
yesterday. (24 hours worth).

The collection of the data is another time consuming task, I'd be
interested if you had some ideas here? I haven't yet looked at how to
write code to retrieve data from the www. When I first started to run
through the tutorials I looked at the 'form', the easy bit :) , but I
reckon you'll tell me I'm making it more difficult than needs be. This
is what it looked like, but I don't think you'd have a url bar at the
top, and I don't need TextBoxes, it could just save to a simple txt
file:

http://www.southendweather.net/VB_Ogimet.pdf

I then started to look at the breakup/differences for each 'send'
which is quite logical, although you'll notice in Spanish. The changes
for each send are: 'Country/Year/Month & Times). I've uploaded a word
file which shows this:

http://www.southendweather.net/ogilist.rtf

This file also lists ALL the Countries I select each time (over 40).
Fortunately, Firefox doesn't reset the times every time you go back
one page, so it's only the Country I have to change each time.
It would be great if I could set in one go ALL the Counties for a
given time span and then it grabbed them all into one file in one hit.
Not sure how flexible it could be, add more Countries perhaps? Any
ideas?

I maybe pushing my luck here, but I very much appreciate what you've
done for me.

Regards

Keith (Southend)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top