Results 1 to 18 of 18

Thread: [RESOLVED] Retrieve a Specific Lines Number

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Resolved [RESOLVED] Retrieve a Specific Lines Number

    Anyone knows how to get the line number of a specific line in a .txt file?

    Code:
    Dim c As Integer = 1
    Dim sr As New System.IO.StreamReader(onlinefileversion)
    Dim line As String = String.Empty
    
    Public Sub versioncheck()
            line = sr.ReadLine.IndexOf(myfileversion)
            Do While (Not line Is Nothing)
                If line.Contains(myfileversion) Then
                    MsgBox("Exitsts")
                Else
                    MsgBox("Does not exist")
                End If
                line = sr.ReadLine()
                c = c + 1
            Loop
        End Sub
    The code reports if the X version exists in a .txt file, but now I also need the lines number for the program to work correctly.
    If my post is unclear, tell me and i will describe it a bit more.

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Retrieve a Specific Lines Number

    You already know this, you have a variable called c, that is the line number. If the first line is #1 to you, then just do the c = c + 1 at the top of the Do While, instead of at the bottom

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Re: Retrieve a Specific Lines Number

    Quote Originally Posted by Grimfort View Post
    You already know this, you have a variable called c, that is the line number. If the first line is #1 to you, then just do the c = c + 1 at the top of the Do While, instead of at the bottom
    Yes but the searched line is not always the #1 line in file.

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Retrieve a Specific Lines Number

    Maybe you are confused, you read a line and +1 to the variable, call that #1, you check if it is your match line. It is not, you read another line and +1 to the variable, call that #2, you check it is your match line. It is not, you read another line and +1 to the variable, call that #3, you check it is your match line, it is! So.. your variable holds the value 3, hence you are on line #3.

    Your code currently adds 1 AFTER the test, so the variable willl be -1 from the actual line number. Hence my suggestion to move it at the top of the loop BEFORE the test.

    Code:
    Dim c As Integer = 1
    Dim sr As New System.IO.StreamReader(onlinefileversion)
    Dim line As String = String.Empty
    
    Public Sub versioncheck()
            line = sr.ReadLine  'Did you really want to get the index (a number) into a string, I thought this a mistype?
            Do While (Not line Is Nothing)
                c = c + 1
                If line.Contains(myfileversion) Then
                    MsgBox("Exitsts on line #" & c.ToString)
                Else
                    MsgBox("Does not exist")
                End If
                line = sr.ReadLine()
            Loop
        End Sub
    Last edited by Grimfort; Aug 12th, 2011 at 05:25 PM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Re: Retrieve a Specific Lines Number

    aaaah... yeah i understand now,
    and yes the IndexOf was a mistype.

    One more question,
    how can I retrieve the text of the line below?
    Sorry for the stupid questions.

  6. #6
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Retrieve a Specific Lines Number

    Your code is in a loop. Usually when you find the info you are after, you stop and Exit Do, to jump out. All you have to do is flag a variable to allow you to continue once more.

    Code:
    Dim bolStopOnNextLine As Boolean
    
    Do While (Not line Is Nothing)
                c = c + 1
                If bolStopOnNextLine Then
                    'We can use the current line and quit
                    MsgBox("We found " & line & " next!")
                    Exit Do
                End If
                If line.Contains(myfileversion) Then
                    MsgBox("Exitsts on line #" & c.ToString)
                    bolStopOnNextLine = True
                Else
                    MsgBox("Does not exist")
                End If
                line = sr.ReadLine()
            Loop
    Something like that, I can't test it currently.

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Re: Retrieve a Specific Lines Number

    Thanks a lot! Works good, except this does not display the correct line number, but thats not important!
    One more thing...how can I, after i got the next lines value and downloaded the file with that name, restart the check and repeat until i ran out of available versions (i got the last part)?
    Sorry for one more dumb question. :/
    Last edited by uroshercog; Aug 14th, 2011 at 03:29 PM.

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Re: Retrieve a Specific Lines Number

    bump? Any help?

  9. #9
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Retrieve a Specific Lines Number

    What have you tried, getting the last value is simple logic. You just keep looping until the end, the last thing you read, is the last value . Just save each line in the loop in a variable, when its done, the variable will contain your data. Have an attempt first.

  10. #10

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Re: Retrieve a Specific Lines Number

    Quote Originally Posted by Grimfort View Post
    What have you tried, getting the last value is simple logic. You just keep looping until the end, the last thing you read, is the last value . Just save each line in the loop in a variable, when its done, the variable will contain your data. Have an attempt first.
    I actually dont need the last value anymore (solved it differently),
    I cant get the loop to repeat when the file extracting finishes.
    All i get is some messages (they just keep repeating and no files are downloaded/extracted)

  11. #11
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Retrieve a Specific Lines Number

    Well, I can not guess what your code looks like, post it!

  12. #12

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Re: Retrieve a Specific Lines Number

    Code:
    Imports Nini.Config
    Imports System.IO
    Imports Ionic.Zip
    Imports System
    
    
    Public Class Main
    #Region "DIMs"
        Dim configFile As New IniConfigSource("update.ini")
        Dim myurl As String = configFile.Configs("CONNECTION").Get("myUrl")
        Dim myupdatefolder As String = configFile.Configs("CONNECTION").Get("myUpdateFolder")
        Dim mynewsurl As String = configFile.Configs("CONNECTION").Get("myNewsUrl")
        Dim myregisterurl As String = myurl + configFile.Configs("CONNECTION").Get("myRegisterUrl")
        Dim myclienttitle As String = configFile.Configs("CLIENT").Get("myClientTitle")
        Dim myclientstate As String = configFile.Configs("CLIENT").Get("myClientState")
        Dim myclientversion As String = configFile.Configs("CLIENT").Get("myClientVersion")
        Dim myfileversion As String = configFile.Configs("FILE").Get("myFileVersion")
        Dim myadvertisementurl As String = configFile.Configs("CONNECTION").Get("myAdvertisementUrl")
        Dim onlinepicturedirectory As String = myurl + myupdatefolder + "\" + "res" + "\"
        Dim onlinefile As String = "version.txt"
        Dim localfile As String = "update.ini"
        Dim path As String = My.Computer.FileSystem.CurrentDirectory + "\"
        Dim krneki As String = False
        Dim c As Integer = 1
        Dim line As String = String.Empty
        Dim newfile As String = line
    
    #End Region
    
        Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            'Če link za novice ne obstaja, preskoči 
            If mynewsurl = "" Then
            Else
                webNews.Url = New Uri(myurl + mynewsurl)
            End If
    
            'Če ime za client ne obstaja, preskoči
            If myclienttitle = "" Then
                Me.Text = Me.Text
            Else
                Me.Text = myclienttitle + Me.Text
            End If
    
            'Prikaži template
            picBackground.ImageLocation = onlinepicturedirectory + "template.png"
            gameExit.ImageLocation = onlinepicturedirectory + "exit.png"
            gameReg.ImageLocation = onlinepicturedirectory + "register.png"
            gameStart.ImageLocation = onlinepicturedirectory + "start.png"
    
            'Prikaži advertisement
            webSide.Url = New Uri(myurl + myadvertisementurl)
    
            'Transparenten lbProg
            lbProg.Parent = picBackground
            lbProg.BackColor = Color.Transparent
    
            'Prevei če file obstaja
            If System.IO.File.Exists(path + onlinefile) Then
                'Če obstaja ga zbriši
                My.Computer.FileSystem.DeleteFile(onlinefile)
            End If
    
            'Prenesi nov file
            My.Computer.Network.DownloadFile _
                (myurl + myupdatefolder + onlinefile,
                 path + onlinefile)
    
            'Začni s preverjanjem verzije ;))
            versioncheck()
    
        End Sub
    
        Public Sub versioncheck()
            Dim sr As New System.IO.StreamReader(onlinefile)
            line = sr.ReadLine
    
            Dim bolStopOnNextLine As Boolean
            Do While (Not line Is Nothing)
                c = c + 1
                If bolStopOnNextLine Then
                    'MsgBox(line)
                    sr.Dispose()
                    downloadUpdates()
                    Exit Do
                End If
                If line.Contains(myfileversion) Then
                    'MsgBox(c.ToString - 1)
                    bolStopOnNextLine = True
                Else
                    'sr.Close()
                    barProg.Value = 100
                    lbProg.Text = "No new updates! Please start the game!"
                    'My.Computer.FileSystem.DeleteFile(onlinefile)
                End If
                line = sr.ReadLine()
            Loop
        End Sub
    
        Private Sub downloadUpdates()
            If My.Computer.FileSystem.CurrentDirectory.Contains(line + ".zip") Then
                My.Computer.FileSystem.DeleteFile(line + ".zip")
            End If
            My.Computer.Network.DownloadFile _
                (myurl + myupdatefolder + line + ".zip", _
                 path + line + ".zip")
            unzipbeggin()
        End Sub
    
        Private Sub unzipbeggin()
            Dim ZipToUnpack As String = My.Computer.FileSystem.CurrentDirectory + "\" + line + ".zip"
            Dim UnpackDirectory As String = My.Computer.FileSystem.CurrentDirectory
            Using zip1 As ZipFile = ZipFile.Read(ZipToUnpack)
                Dim e As ZipEntry
                For Each e In zip1
                    e.Extract(UnpackDirectory, ExtractExistingFileAction.OverwriteSilently)
                    barProg.Value = 90
                    lbProg.Text = "Updating..."
                Next
                barProg.Value = 100
                lbProg.Text = "Update Finished! Please restart the updater!"
                gameStart.Hide()
                Dim Lines() As String = System.IO.File.ReadAllLines(localfile)
                Lines(13) = "myFileVersion = " + line
                System.IO.File.WriteAllLines(localfile, Lines)
            End Using
            My.Computer.FileSystem.DeleteFile(line + ".zip")
            My.Computer.FileSystem.DeleteFile(onlinefile)
        End Sub
    
        Private Sub gameReg_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles gameReg.MouseClick
            Process.Start(myregisterurl)
        End Sub
    
        Private Sub gameStart_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles gameStart.MouseClick
            Process.Start("system\Game.exe", myclientversion)
        End Sub
    
        Private Sub gameExit_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles gameExit.MouseClick
            Me.Close()
        End Sub
    
    End Class
    The entire work so far

  13. #13
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Retrieve a Specific Lines Number

    I can not tell what you code is doing as I do not have your file, however I can tell you what it might do:

    Code:
    Public Sub versioncheck()
            Dim sr As New System.IO.StreamReader(onlinefile)
            line = sr.ReadLine
    
            Dim bolStopOnNextLine As Boolean
            Do While (Not line Is Nothing)
                c = c + 1
                If bolStopOnNextLine Then
                    'MsgBox(line)
                    sr.Dispose() ****4****
                    downloadUpdates()
                    Exit Do ****2****
    
                End If
                If line.Contains(myfileversion) Then ****1****
                    'MsgBox(c.ToString - 1)
                    bolStopOnNextLine = True
                Else
                    'sr.Close()
                    barProg.Value = 100 ****3****
                    lbProg.Text = "No new updates! Please start the game!"
                    'My.Computer.FileSystem.DeleteFile(onlinefile)
                End If
                line = sr.ReadLine()
            Loop
        End Sub
    1) Here you are checking if the line contains your version, you are not actually extracting the version, just setting a flag. From this I guess that the next line contains the actual line of data you use in the zip extract part.

    2) You are jumping out of the loop. At this point you stop any kind of processing with this file, is that what you want to do? The line variable, should contain the next line as we spoke about earlier, does it? You only call versioncheck() 1 times, so there should be no looping whatever of this file other than at the start.

    3) For EVERY line that does not match your version, you are changing the status as failed to download updates. If your version number is on say line #4 then for 3 lines you are saying that there are no updates! What I believe you should be doing is setting a flag to say that you have or have not extracted version info, and then set that updated message AFTER the loop.

    4) You are disposing the file while inside the loop, no where else. If the file does not ever contain a version number, you will never dispose of it!

  14. #14
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Retrieve a Specific Lines Number

    Something like this:

    Code:
    Public Sub versioncheck()
    
            Dim sr As New System.IO.StreamReader(onlinefile)
            Dim FileLine As String = sr.ReadLine
    
            Dim bolStopOnNextLine As Boolean
    	Dim bolFoundUpdate As Boolean
    	Dim intLineCount As Integer
    
            Do While (Not FileLine Is Nothing)
                intLineCount += 1
                If bolStopOnNextLine Then
    		 		
     		MsgBox("FileLine on line " & intLineCount.ToString)
                    downloadUpdates(FileLine) '<< pass the line to the function, do not use a public!
                    Exit Do 
                End If
                If line.Contains(myfileversion) Then
                    bolStopOnNextLine = True  'Mark to stop after the next loop
    		bolFoundUpdate = True 'Mark that we did find a version text line
    		MsgBox("Version on line " & intLineCount.ToString)
                End If
                FileLine = sr.ReadLine()
            Loop
    
    	sr.Close
    	sr.Dispose
    
    	'Only update the UI if we did not find any updates
    	If Not bolFoundUpdate Then
               barProg.Value = 100
               lbProg.Text = "No new updates! Please start the game!"
    	End If
    
        End Sub
    Note: I can not test it, I do not have your file!

  15. #15

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Re: Retrieve a Specific Lines Number

    Thanks for the explanation! I never realized i had so much mistakes O.O
    If you want you can test it -> http://www.megaupload.com/?d=SH472NKS
    Last edited by uroshercog; Aug 16th, 2011 at 06:52 AM.

  16. #16
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Retrieve a Specific Lines Number

    You will see the mistakes you make yourself, if you step through your code. If this is not something you have done before, then just click on the left had side of a line of code and it will be marked red. When the code hits this, you can step-into each line of code and see what it is doing.

  17. #17

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Re: Retrieve a Specific Lines Number

    Actually this is my 2nd project in Visual Basic, so im not so familiar with all the functions i can/could use
    For now im error-free

  18. #18
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Retrieve a Specific Lines Number

    When your happy mark the thread as resolved and open new ones if you have different questions. You can always link back here to give history.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width