Results 1 to 12 of 12

Thread: [RESOLVED] How to count the lines of a text file situated on a server

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Resolved [RESOLVED] How to count the lines of a text file situated on a server

    How would I count the lines of a text file situated on a server?

    I know that if the file is on my pc I just do this:

    Code:
            Dim lines As String() = IO.File.ReadAllLines("C:\mytextfile.txt")
            msgbox("The text file has " & lines.Length & " lines.")
    But what about if the file is on a server such as "http://www.mywebsite.com/mytextfile.txt"?

    If try to download the file in memory and read it, it works fine:

    Code:
    Dim textfilet As String = client.DownloadString("http://www.mywebsite.com/mytextfile.txt")
                MsgBox(textfile)
    But I just cannot find a way to count its lines.

    Thank you in advance,

    Andrea

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: How to count the lines of a text file situated on a server

    you can't get the count of lines because you downloading it as a string...

    this is one way to do it:
    first you download the file and save it, then you read it
    Code:
      Dim client As New System.Net.WebClient
            client.DownloadFile("http://wwww.MyWebSite/test.txt", "test.txt")
            Dim lines As String() = IO.File.ReadAllLines("test.txt")
            MessageBox.Show(lines.Length)
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: How to count the lines of a text file situated on a server

    Try this:

    Code:
                Using WC As New Net.WebClient
                    Dim myFileContents As String = WC.DownloadString("URL TO TEXT FILE HERE")
    
                    Dim myFileLines() As String = myFileContents.Split(New String() {Environment.NewLine}, System.StringSplitOptions.None)
    
                    MsgBox("The text file has " & myFileLines.Length & " lines.")
                End Using

  4. #4
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: How to count the lines of a text file situated on a server

    well.. it appears that you CAN get the count of lines even if you download it as a string, here's how I did it:
    Code:
       Dim client As New System.Net.WebClient
            Dim file As String = client.DownloadString("http://www.MyWebSite.com/test.txt")
            Dim lines As String() = file.Split(Environment.NewLine)
            MessageBox.Show(lines.Length)
    you just split the string in the last byte of the line (0x0)

    I'm not sure how secure this method is, but it works at least for what i tested.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  5. #5
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: How to count the lines of a text file situated on a server

    heh kleinma beat me :B
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: How to count the lines of a text file situated on a server

    Quote Originally Posted by motil View Post
    well.. it appears that you CAN get the count of lines even if you download it as a string, here's how I did it:
    Code:
       Dim client As New System.Net.WebClient
            Dim file As String = client.DownloadString("http://www.MyWebSite.com/test.txt")
            Dim lines As String() = file.Split(Environment.NewLine)
            MessageBox.Show(lines.Length)
    you just split the string in the last byte of the line (0x0)

    I'm not sure how secure this method is, but it works at least for what i tested.
    There isn't a string.split() method that just takes a string parameter to split on. Only char, or you need to pass in the split options enum flag (see my example)

  7. #7
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: How to count the lines of a text file situated on a server

    well I tested it and it works..
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  8. #8
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: How to count the lines of a text file situated on a server

    Quote Originally Posted by motil View Post
    well.. it appears that you CAN get the count of lines even if you download it as a string, here's how I did it:
    Code:
       Dim client As New System.Net.WebClient
            Dim file As String = client.DownloadString("http://www.MyWebSite.com/test.txt")
            Dim lines As String() = file.Split(Environment.NewLine)
            MessageBox.Show(lines.Length)
    you just split the string in the last byte of the line (0x0)

    I'm not sure how secure this method is, but it works at least for what i tested.
    Hmm, just wanted to add that Environment.NewLine is CRLF - for Windows.
    There are OSs when only CR or only LF is used. If that text file was created in some other environment, this method wouldn't work.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: How to count the lines of a text file situated on a server

    Thank you guys!

    both the codes you gave me works!

    But I was trying count the lines of two text files on a server and return the count in only one text box.
    Just to give you an example of what I am trying to do:

    Code:
            Dim clientR As New System.Net.WebClient
            Dim clientB As New System.Net.WebClient
            Dim Rfile As String = clientEN.DownloadString("http://mywebsite.com/redcarlist.txt")
            Dim Bfile As String = clientIT.DownloadString("http://mywebsite.com/blackcarlist.txt")
            Dim Rlines As String() = Rfile.Split(Environment.NewLine)
            Dim Blines As String() = Bfile.Split(Environment.NewLine)
            MsgBox("There are " & Rlines.Length & " red cars in the list", vbNewLine, "There are " & Blines.Length & " black cars in the list")

    Any idea on how to do that?

    Thank you,

    Andrea

  10. #10
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: How to count the lines of a text file situated on a server

    not the ideal way of doing this but ...
    Code:
      TextBox1.Multiline = True
            TextBox1.Height = 40
            TextBox1.Width = 200
            TextBox1.Text = String.Format("There are {0}red cars in ths list{1} there are {2} cars in the list", Rlines.Length, Environment.NewLine,  Blines.Length)
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: How to count the lines of a text file situated on a server

    Sorry but I do not understant.

    Why did you put "textbox1"? I do not have textboxes in my form

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: How to count the lines of a text file situated on a server

    Solved, it was easy but I was tierd and couldn't figured it out:

    Code:
            Dim client1 As New System.Net.WebClient
            Dim file1 As String = client.DownloadString("URL/file1")
            Dim lines1 As String() = file1.Split(Environment.NewLine)
            Dim client2 As New System.Net.WebClient
            Dim file2 As String = client2.DownloadString("URL/file2")
            Dim lines2 As String() = file2.Split(Environment.NewLine)
            MsgBox("There are " & lines1.Length & " red cars" & vbNewLine & "There are " & lines2.Length & " black cars")

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