Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi I currently have the following code , which opens a tab delimted text file and sends out a sms per line in the file. The file looks as follows :

Mobile #tab# Message
+12345678 #tab# Message

The message is the same on all lines only the mobile number changes.

The site I use allows upto 99 mobile numbers to be entered separared by a comma.


So what I want to do it read through the file , get the 1st lot 99 mobile numbers and enter them into {strmsg} separated by comma"s.Submit that to the site and then get the next 99 numbers etc.

Not really sure where to start , have been trying somthing like this , which does start to get me the sting containing the mobile numbers , but then I seem to end up with a loop within a loop and don't know what to do next.Just looking for some advice on the best way to do this.

VB
Do While (objSR.Peek <> -1)

            strline = objSR.ReadLine
            fileline = strline.Split(vbTab)

            Dim num As String


            num = num + "," _
              & fileline(0)

        Loop





Current Code
VB
Dim objFS As New FileStream("c:\test.sms", FileMode.Open, FileAccess.Read)
        Dim objSR As New StreamReader(objFS)

        Dim strline, fileline() As String
        Do While (objSR.Peek <> -1)

            strline = objSR.ReadLine
            fileline = strline.Split(vbTab)

            Dim strnum As String = fileline(0)
            Dim strmsg As String = fileline(1)

            Dim request As HttpWebRequest
            Dim response As HttpWebResponse = Nothing

            Dim url As String
            Dim host As String = "http://southafrica.msg91.com"
            Dim senderid As String = "Test"

            Try

                url = host + "/sendhttp.php?" _
                         & "user=" & HttpUtility.UrlEncode(user) _
                         & "&password=" + HttpUtility.UrlEncode(password) _
                         & "&mobiles=" + HttpUtility.UrlEncode(strnum) _
                         & "&message=" + HttpUtility.UrlEncode(strmsg) _
                         & "&sender=" + HttpUtility.UrlEncode(senderid)



                request = DirectCast(WebRequest.Create(url), HttpWebRequest)

                response = DirectCast(request.GetResponse(), HttpWebResponse)

            Catch ex As Exception
                MsgBox(ex.Message)
            
        Loop
Posted
Comments
[no name] 23-Aug-12 12:05pm    
Is there some reason that you are not using File.ReadAllLines?
Member 8727996 23-Aug-12 12:30pm    
The file contains anywhere from a 1000 to 2000 lines.Whould I be able to use ReadAllLines on that many.

I hope I understand your question right. Your current code sends the sms one by one, and you want to send 99 smss in one. If that is your intention you were half way down. You could change your sending code to

VB
Dim objFS As New FileStream("c:\test.sms", FileMode.Open, FileAccess.Read)
Dim objSR As New StreamReader(objFS)
Dim counter As Int32 = 1
Dim num As System.Text.StringBuilder = New System.Text.StringBuilder(200)

Dim strline, fileline() As String
Do While (objSR.Peek <> -1)

    strline = objSR.ReadLine
    fileline = strline.Split(vbTab)

    Dim strnum As String = fileline(0)
    Dim strmsg As String = fileline(1)

    If counter > 1 Then
        num.Append(",")
    End If
    num.Append(strnum)

    If (counter Mod 99 = 0) OrElse (objSR.EndOfStream) Then
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing

        Dim url As String
        Dim host As String = "http://southafrica.msg91.com"
        Dim senderid As String = "Test"

        Try

            url = host + "/sendhttp.php?" _
                     & "user=" + HttpUtility.UrlEncode(user) _
                     & "&password=" + HttpUtility.UrlEncode(password) _
                     & "&mobiles=" + HttpUtility.UrlEncode(num.ToString()) _
                     & "&message=" + HttpUtility.UrlEncode(strmsg) _
                     & "&sender=" + HttpUtility.UrlEncode(senderid)



            request = DirectCast(WebRequest.Create(url), HttpWebRequest)

            response = DirectCast(request.GetResponse(), HttpWebResponse)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        counter += 1
        num = New System.Text.StringBuilder(200)
    End If


Loop


I've intergrated your loop with your current code using the mod statement to test if 99 messages are read and the StreamReader.EndOfStream to test if the end of the stream is reached.

I've change the 'num as String' to a 'num as StringBuilder'. If you are concatinating a lot of string values the StringBuilder has a beter preformence (and memory usage).

I couldn't test the code as it might send data. I hope it will work.
 
Share this answer
 
Comments
Member 8727996 27-Aug-12 10:17am    
Thanks Martijn , Your Code worked perfectly ,only thing I needed to do was move Counter +=1 under the line "num.Append(strnum)"
Martijn Kok 28-Aug-12 7:00am    
You're right. I'm glad it worked.
Is the file huge ? Like Wes said you can use file.ReadAllLines and only parse 99 of them. Otherwise you need to write your own code to open the file and read one line at a time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900