|
-
Feb 8th, 2011, 09:43 AM
#1
Thread Starter
Lively Member
[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
-
Feb 8th, 2011, 09:57 AM
#2
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 
-
Feb 8th, 2011, 10:02 AM
#3
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
-
Feb 8th, 2011, 10:02 AM
#4
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 
-
Feb 8th, 2011, 10:03 AM
#5
Re: How to count the lines of a text file situated on a server
* 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 
-
Feb 8th, 2011, 10:04 AM
#6
Re: How to count the lines of a text file situated on a server
 Originally Posted by motil
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)
-
Feb 8th, 2011, 10:05 AM
#7
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 
-
Feb 8th, 2011, 10:16 AM
#8
Re: How to count the lines of a text file situated on a server
 Originally Posted by motil
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.
-
Feb 8th, 2011, 10:30 AM
#9
Thread Starter
Lively Member
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
-
Feb 8th, 2011, 10:40 AM
#10
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 
-
Feb 8th, 2011, 10:46 AM
#11
Thread Starter
Lively Member
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
-
Feb 8th, 2011, 06:49 PM
#12
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|