PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Searching in text file.-VBForums
Results 1 to 11 of 11

Thread: Searching in text file.

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2013
    Posts
    37

    Searching in text file.

    Hello,

    Due to I am implementing an application with the reason to not access to the code after the implementation.

    I need to make the code read a text file.

    I use it the streamreader for others things, but I need to find the way to do something more that will help me a lot to transfor the application.

    This application has to sent templates to differents email address and also has links to differents websites.

    What I need is to find the way, which I cant find a help in google, to get information from the text file to copy that information in memory in load of the program.

    My thinking is into the text something like:

    emailadd1 = "john@gmail.com"
    emailadd2 = "example@gmail.com"

    When the applition load will read the file and the value emailadd1 into the vb.net code will become "john@gmail.com" the same as in the text.

    Is that possible?

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,311

    Re: Searching in text file.

    If we can put a man on the moon i think it's possible to read and search a textfile dont you agree? If you want to read a textfile into an array you would use io.file.readalllines method.

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2013
    Posts
    37

    Re: Searching in text file.

    Hello ident,

    I am trying to find examples in internet. But I cant. Do you mind to give me some examples?

    Sorry for this.

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,311

    Re: Searching in text file.

    you must be one of the few where Google does not work. When you Google io.file.readalllines its funny how the top result is the documentation

    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Searching in text file.

    Examples of what?

    Dim AllLines() As String = IO.File.ReadAllLines("yourfilename.txt")

    That's all there is to it. You now have an array of strings representing each line in the file. What the lines contain only you know but you simply manipulate them like any other String.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2013
    Posts
    37

    Re: Searching in text file.

    Hello,

    This is what I am trying to do.

    Into the test.txt file there is this:

    [emailaddr1] test@test.com

    If I use this code
    Code:
     test.Text = s.Substring(s.LastIndexOf(" "))
    it works.

    But I need to finde this value of a variable into test.txt

    I have try this but does not work.

    Code:
            Dim emailadd As String = "[emailaddr1]"
            Dim path As String = "C:\Users\joserodriguezan\Desktop\links.txt"
            Dim readText() As String = File.ReadAllLines(path)
            Dim s As String
            For Each s In readText
                test.Text = s.Substring(s.IndexOf(emailadd))
            Next

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Searching in text file.

    Sorry but you're going to have to explain this a bit better than this. Given the line ... [emailaddr1] test@test.com ... exactly what do you want to appear in the textbox and what, if anything do you want to do subsequently?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2013
    Posts
    37

    Re: Searching in text file.

    Sure,

    I am changing an application that sent emails templates.

    Those templates are sent with

    Code:
    item.SentOnBehalfOfName = "emailaddress@test.com"
    But I dont want to take the code everytime procedures change, so I want that the application read a text file with this content:

    [Departmentemail1] test1@test1.com
    [Departmentemail2] test2@test2.com
    [Departmentemail3] test3@test3.com

    So people with editor rights can change the email, from the text file.

    On the code will be variables as this

    Code:
    Dim Departmentemail1, Departmentemail2, Departmentemail3 as string
    And at the load of the application it reads text file

    and then the values of the variables will be the email address of the txt file.

    Sorry for my english, if it is not understandable.

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Searching in text file.

    I'd recommend that you use a Dictionary for this.

    vb.net Code:
    1. Public Class Form1
    2.  
    3. ' create dictionary (key, value)
    4.     Dim EMails As New Dictionary(Of String, String)
    5.  
    6.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    7.  
    8.         Dim lines = IO.File.ReadAllLines([I]filename[/I])
    9.         For Each ln As String In lines
    10.             Dim splt = ln.Split(" "c) ' split line into two halves
    11.         ' exclude square brackets from first entry and add as key
    12.         ' add second entry as value
    13.             EMails.Add(splt(0).Substring(1, splt(0).Length - 2), splt(1))
    14.         Next
    15.        ' access values by using key to return value
    16.         Me.Text = EMails("Departmentemail2")
    17.  
    18.     End Sub
    19.  
    20. End Class
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2013
    Posts
    37

    Re: Searching in text file.

    Hi dunfiddlin,

    Thank you for your answer.

    I have create an xml file like this.

    Code:
    Private Sub savebt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles savebt.Click
    02	      'Generar y modifica archivo xml
    03	 
    04	      Dim obj As Object
    05	      Dim archivo As Object
    06	      Dim x As Integer = 0
    07	      Dim CC1email, GenESCemail, SAPEmail, CloseIncEmail As String
    08	      CC1email = CC1emailtxtbx.Text
    09	      GenESCemail = GenESCemailtxtbx.Text
    10	      SAPEmail = SAPESCemailtxtbx.Text
    11	      CloseIncEmail = CloseEmailtxtbx.Text
    12	 
    13	      obj = CreateObject("Scripting.FileSystemObject")
    14	 
    15	      archivo = obj.CreateTextFile(filexml, True)
    16	      archivo.WriteLine("<?xml version='1.0' encoding='utf-8'?>")
    17	      archivo.WriteLine("<G>")
    18	      archivo.WriteLine("<Imagen id='" & CStr(x) & "'>")
    19	      archivo.WriteLine("<Email>" & CC1email & "</Email>")
    20	      archivo.WriteLine("<Email>" & GenESCemail & "</Email>")
    21	      archivo.WriteLine("<Email>" & SAPEmail & "</Email>")
    22	      archivo.WriteLine("<Email>" & CloseIncEmail & "</Email>")
    23	      archivo.WriteLine("</Imagen>")
    24	      archivo.WriteLine("")
    25	 
    26	      archivo.WriteLine("</G>")
    27	      archivo.close()
    28	      CC1emailtxtbx.Text = ""
    29	      GenESCemailtxtbx.Text = ""
    30	      SAPESCemailtxtbx.Text = ""
    31	      CloseEmailtxtbx.Text = ""
    32	      LoadXMLEsc()
    33	  End Sub
    But the problem is that I cant modify each data separately.

  11. #11
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Searching in text file.

    I have create an xml file like this.
    Well technically you've created a text file that happens to look like an xml file but let's not quibble over it since you completely failed to mention that this was your intention in any of the previous posts!

    But the problem is that I cant modify each data separately.
    Yes. That's kind of the point of xml files which are merely repositories of a fixed state of an object. You manipulate the original object until it reaches a state that you want to save and then commit the whole thing to the xml. When you want to further manipulate the object you reinitalise it with the data from the xml file. Changing individual values directly within a file is at best inefficient and always prone to error or corruption of the data.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width