Replace text in a file from Textbox input


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Replace text in a file from Textbox input

  1. #1
    Join Date
    Apr 2008
    Posts
    4

    Replace text in a file from Textbox input

    Hi,

    I am new to Visual basic and I am trying to write a program. But I am having problems modifing a file.

    I have a static textfile with "code words" to replace from input from various textboxes.

    Say I have a file that look like this (c:\kaka.txt)

    how many users: code1
    Whats the color: code2
    The best TV-game: code3

    And i have made a VB-application with 3 Textboxes for input by user.

    Then I want to replace the string/word code1 in kaka.txt with TextBox1.text and so on..... how do I do it.

    Please be very specific with code example.

    Thank you very much for ALL response!!

  2. #2
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    Reading and writing to files can be tricky. Basically a text file is just one long stream of characters with carriage return(Cr) and line feed(Lf) characters located where the end of each line should be. So for example in a text box you have:
    Hello
    There
    Which would look like:
    HelloCrLfThereCrLf
    inside a text file.
    If you want to change something in the file you must read in the entire file and then write all of your changes back to the file. The reason for this is because any changes you make may remove and/or insert additional characters. When you do this all of the other characters in the file must be moved around to adjust for the changes.
    There are two ways to alter the contents of your file. You can read it all into memory, that is variables in your program, and then write the new data back to the file. Or you can create new file, read in the old file a chunck at a time, make any needed changes to each chunck, and write each chunck to the new file. Once everything has been read from the old file and written to the new file, you can then delete the old file, and simply rename the new file to give it the same name as the old one.
    Both methods are good depending on the size of the file. A small file works well to just read in the whole thing and write it all back when you are done. But large files may be too big to store in memory so processing a chunck at a time is a better way to go.
    In your case is looks like the file is very small, so loading it all into memory should not be a problem. If you open the file in binary mode you can read the entire contents into a string. Once you have the file loaded into your string you can use the Split() function, with CrLf as the seperator characters, to split each line into an Array of strings, where each element of the array would hold one line from the file. Now each element of the Lines Array you can be Split again, using the colon as the seperator, into another string array where the first element represents the description of the code and the second element contains the code. If you don't want to use a second array, you could also use the InStr() funstion to find where the colon is, and therefore each character after that would be the code.
    Once you have each code, you can write it to it's corrisponding text box. When you are ready to write the data back, you replace the
    old codes with the new ones from the text boxes. You do this by either setting the second element of the array created by the second Split(), or set the line arrays element string, just past the colon, to the new code.
    After all of the lines have been updated use the Join() function, with the Lines Array and CrLf as the seperator, to put the lines back together in to a single string. Then write the new string back to the file.
    VB Statements and Functions you will need for this are: Open #, Get #, Put #, Close #, EOF(), LOF(), Split(), Join(), InStr(), Mid()
    Others that might be of interest for the future:Print #, Input #, Line Input #, Left(), Right(), Trim(), LTrim(), RTrim(), Replace()
    This should be enough information to get you started. Do as much as you can, when or If you get stuck, Post what you have so far and we will help you from there.

  3. #3
    Join Date
    Apr 2008
    Posts
    4
    Hi and thank you for your reply!! Ive looked it thru a couple of times but im just to newbie with this. I need some code to work on. I cant solve this one =((

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    In this instance, as the text file is so small, perhaps another way to go would be to read the text file into a multiline textbox, make your changes, delete the original text file, then write the contents of the textbox out as a new file (with the same name as the old one.)
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  5. #5
    Join Date
    Apr 2008
    Posts
    4
    Thanks Hack, but this needs to be user friendly and the textfile isnt really that simple as the example shows. So i need to stick to my input fields(textboxes) 18 of them. and 4 checkboxes...

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Then my suggestion would be to move away from using a text file for this and go to a database. It is much, much easier to manipulate and is also more secure.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  7. #7
    Join Date
    Apr 2008
    Posts
    4
    The thing is that i want it to be a file. Because that file will later on be transfered via sftp to a server. Otherwise it would have been i nice idea.

    If i used a database i would need to take valuse from the database and replace them to the file anyways.....

    Is it so tricky to modify files with Visual Basic? I thought it was just my newbieness ....

  8. #8
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    Normally if someone is stuck I just give them the code they need to get past the stuck point. If they post their code, I would make corrections and post it back. This way both you and anyone else reading this thread would learn how to do this on their own. Beleave me the more you can do yourself, even with our help, the more you will learn and remember, the next time a similar project comes up. So here are a few more hints:
    This code will open the file and split it into an Array of lines.
    Code:
    Dim buf As String
    Dim Lines() As String
    
      'Open file in binary mode
      Open file For Binary As #1
      
      'set buffer size
      buf = String(LOF(1), " ")
    
      'read contents of file to buffer
      Get #1, 1, buf
    
      'split buffer into Lines Array
      Lines = Split(buf, vbCrLf)
    This code will save the Lines Array back to the file
    Code:
      'Rebuild buffer by Joining Lines Array
      buf = Join(Lines(), vbCrLf)
        
      'if new data is smaller than
      'the contents of the old file
      'then delete the old file
      If Len(buf) < LOF(1) Then
        Kill file
      End If
      
      'Open file in binary mode
      Open file For Binary As #1
    
      'post to file
      Put #1, 1, buf
    So now you need to seperate the codes from each line and put them into the appropriate text box, as well as the reverse. Get each code from each text box and update the code in the appropriate line.
    Oh and the word file in the Open statement is actually a string that I previously set to:
    App.Path & "\kaka.txt"
    One Final hint. I used two routines, one the get the codes and put them into the text boxes, which I call from the Form Load Event, and the other to save the codes from the text boxes, which I call from the Click event of a command button.
    Last edited by Ron Weller; 04-04-2008 at 12:34 PM.

  9. #9
    Join Date
    Mar 2008
    Posts
    24

    Confused...

    Are you saying that you have multiple text boxes and you want them all to be saved into a text file? I'm going to just assume that that is what you are asking and tell you the code now, because if I wait, I will probably forget to check back.

    This code will do it, but it is probably one of the most primitive ways to do this on visual basic.

    Code:
    Dim symbol As String
    
    Private Sub Command1_Click()
    'This is to save
    RichTextBox1.Text = Text1.Text & symbol & Text2.Text & symbol & Text3.Text & symbol
    RichTextBox1.SaveFile "C:\kaka.txt"
    End Sub
    
    Private Sub Command2_Click()
    'this is to load
    RichTextBox1.LoadFile "C:\kaka.txt"
    Text1.Text = Left(RichTextBox1.Text, InStr(RichTextBox1.Text, symbol) - 1)
    RichTextBox1.Text = Right(RichTextBox1.Text, Len(RichTextBox1.Text) - (InStr(RichTextBox1.Text, symbol) + (Len(symbol) - 1)))
    Text2.Text = Left(RichTextBox1.Text, InStr(RichTextBox1.Text, symbol) - 1)
    RichTextBox1.Text = Right(RichTextBox1.Text, Len(RichTextBox1.Text) - (InStr(RichTextBox1.Text, symbol) + (Len(symbol) - 1)))
    Text3.Text = Left(RichTextBox1.Text, InStr(RichTextBox1.Text, symbol) - 1)
    RichTextBox1.Text = Right(RichTextBox1.Text, Len(RichTextBox1.Text) - (InStr(RichTextBox1.Text, symbol) + (Len(symbol) - 1)))
    End Sub
    
    Private Sub Form_Load()
    'you can change the symbol to anything you want, and the less likely someone is to type it, the better. It can get screwed up if it is typed into a text box. I have also put a text checker to not allow the symbol to appear
    symbol = ">}---{<"
    RichTextBox1.Visible = False
    End Sub
    
    Private Sub Text1_Change()
    If InStr(Text1.Text, symbol) > 0 Then Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1): MsgBox "Contains invalid string!"
    End Sub
    
    Private Sub Text2_Change()
    If InStr(Text2.Text, symbol) > 0 Then Text2.Text = Left(Text2.Text, Len(Text2.Text) - 1): MsgBox "Contains invalid string!"
    End Sub
    
    Private Sub Text1_Change()
    If InStr(Text2.Text, symbol) > 0 Then Text2.Text = Left(Text2.Text, Len(Text2.Text) - 1): MsgBox "Contains invalid string!"
    End Sub

Similar Threads

  1. writing to a text file
    By persian_celina in forum .NET
    Replies: 0
    Last Post: 02-19-2006, 03:06 AM
  2. Replies: 1
    Last Post: 01-12-2006, 07:42 PM
  3. wav file fade in out help
    By jase_dukerider in forum C++
    Replies: 2
    Last Post: 04-14-2005, 08:48 PM
  4. Replies: 1
    Last Post: 03-23-2005, 12:18 AM
  5. Replies: 0
    Last Post: 04-17-2000, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center