Results 1 to 11 of 11

Thread: Replacing Text

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    8

    Post Replacing Text

    Hello everyone!

    I have created a fairly basic application in vb6 which is able to open a text file, load it into a text box control and successfully save it again. So far so good...

    What I need to do is find text in this text file and replace it with something new. This part isn't too hard for me, but what is hard is that I have to search for a specific word then replace the integer after it but multiply it first... As lost as I am? Here's what I mean:

    I have a text file with a list of variables.

    var1=12345
    var2=12345
    var3=12345
    etc.

    So what I need to do it find var2 and change just the integers after it. But before I change them, I have a text box in which the user will input a number. Then the var2=# need to be multiplied by text box number. So if the user enters, "5" then it should search for var2 and multiply 12345 * 5 and return a value of 61725. (for example)

    So again, the user can load a text file full of variables, then set a number to multiply a specific variable by and the program will find all the variables with that name in the file and replace them with the new value.

    If anyone can help me dive in to this one it would be greatly appreciated...

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Replacing Text

    Split your textbox content using vbNewLine, Loop the resulting array.
    Split each string inside the loop using '=', the first item of the array retuned is the Variable name, if its the one you are looking for, take the second item of the array (12345), multiply it by the number.
    put the Varname & "=" & the result obtained in that position of your original array.
    Clear the text box and fill it again with the content of your original array (the first).
    Last edited by jcis; Dec 30th, 2005 at 02:43 AM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    8

    Re: Replacing Text

    Quote Originally Posted by jcis
    Split your textbox content using vbNullString, Loop the resulting array.
    Split each string inside the loop using '=', the first item of the array retuned is the Variable name, if its the one you are looking for, take the second item of the array (12345), multiply it by the number.
    put the Varname & "=" & the result obtained in that position of your original array.
    Clear the text box and fill it again with the content of your original array (the first).
    Thanks for the response jcis. Although, I have another question regarding your reply. The text file I am editing is very large, around 3mbs to be exact. And there are over 1500+ of these variables. So my question is, upon "refreshing" the loaded text would this cause serious performance issues, and what would be the most efficient way of temporarily storing this large data? Thanks again!

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Replacing Text

    Ok, I'll give you half of it, you complete it
    VB Code:
    1. Private Sub ChangeValue(pvarName As String, pMultiply As Long)
    2. Dim lposVar         As Long
    3. Dim lposValue       As Long
    4. Dim lposEndLine     As Long
    5. Dim lValue          As Long
    6.  
    7.     lposVar = InStr(1, Text1.Text, pvarName) 'Get the position of the Variable
    8.     lposEqualSign = InStr(lposVar, Text1.Text, "=") 'get the position of = sign
    9.     lposEndLine = InStr(lposEqualSign, Text1.Text, vbNewLine) 'Get the end of the line position
    10.    
    11.     lValue = CLng(Mid$(Text1.Text, lposEqualSign + 1, lposEndLine - lposEqualSign)) 'Get your Value
    12.    
    13.     '.. Multiply your Value
    14.  
    15.     ' Use Left$, your Value and Right$ to finish
    16. End Sub

    EDIT: 1500 lines in a txt file could never reach 3 MB, if the file size is 3 MB there must be something else inside that file.
    Last edited by jcis; Dec 30th, 2005 at 04:22 AM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    8

    Re: Replacing Text

    Quote Originally Posted by jcis
    EDIT: 1500 lines in a txt file could never reach 3 MB, if the file size is 3 MB there must be something else inside that file.
    Sorry, I mean there are over 1500+ variables that I need to change. There is much more text that just what I am changing. Also I am working on implementing this code now, I will update the progress asap. Thank you or your time.

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    8

    Re: Replacing Text

    Ok so, I have it working. I just used the replace funtion:

    VB Code:
    1. Public Sub ChangeValue(pvarName As String, pMultiply As Long)
    2. Dim lposVar         As Long
    3. Dim lposValue       As Long
    4. Dim lposEndLine     As Long
    5. Dim lValue          As Long
    6. Dim lNewValue       As Long
    7.  
    8.     lposVar = InStr(1, txtScript.Text, pvarName) 'Get the position of the Variable
    9.     lposequalsign = InStr(lposVar, txtScript.Text, "=") 'get the position of = sign
    10.     lposEndLine = InStr(lposequalsign, txtScript.Text, vbNewLine) 'Get the end of the line position
    11.    
    12.     lValue = CLng(Mid$(txtScript.Text, lposequalsign + 1, lposEndLine - lposequalsign)) 'Get your Value
    13.    
    14.     lNewValue = lValue * MultiValue
    15.    
    16.     txtScript.Text = Replace(txtScript.Text, pvarName & "=" & CStr(lValue), pvarName & "=" & CStr(lNewValue))
    17.  
    18.  
    19.    
    20. End Sub

    Ok so now another question... I use common dialog to open the text file. I then load this text file to a text box. All fine, but for some reason this file is so large, it won't all load to the text box. Not even half. Is there a way around this? I even tried rich text box, and I got the same result. Any ideas? Also if you're wondering how I load it, here is the code:

    VB Code:
    1. 'Uses the With funtion
    2.  
    3.     txtDir.Text = .FileName
    4.     FileName = cd1.FileName
    5.     f = FreeFile
    6.    
    7.     Open FileName For Input As f
    8.     rtb1.Text = Input$(LOF(f), f)
    9.     Close f

    Thanks for your time.

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Replacing Text

    Try using something like this to read the data into a RTB. It has unlimited size, so that can't be the problem.

    VB Code:
    1. Private Sub OpenFile()
    2.    ' The RichTextBox control is named "rtfData."
    3.    ' The CommonDialog is named "dlgOpenFile."
    4.    ' Declare a String variable for file name.
    5.    ' Show the Open File dialog box, and set the
    6.    ' variable to the filename.
    7.    Dim strOpen As String
    8.    dlgOpenFile.ShowOpen
    9.    strOpen = dlgOpenFile.FileName
    10.    ' Use LoadFile method to open the file.
    11.    rtfData.LoadFile strOpen
    12. End Sub

    The RTB can save the file as .Txt or .Rtf

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    8

    Re: Replacing Text

    Sorry my mistake. The file I was loading into the program was only a piece of the file. lol So it does actaully load fully. But when I edit the file with the multiplier, it locks up. So, instead of making it try to change all 1500+ variables at once, would it be smarter to put this in a loop so it changes one by one? Maybe with a progress bar?

    VB Code:
    1. Public Sub ChangeValue(pvarName As String, pMultiply As Long)
    2. Dim lposVar         As Long
    3. Dim lposValue       As Long
    4. Dim lposEndLine     As Long
    5. Dim lValue          As Long
    6. Dim lNewValue       As Long
    7.  
    8.     lposVar = InStr(1, txtScript.Text, pvarName) 'Get the position of the Variable
    9.     lposequalsign = InStr(lposVar, txtScript.Text, "=") 'get the position of = sign
    10.     lposEndLine = InStr(lposequalsign, txtScript.Text, vbNewLine) 'Get the end of the line position
    11.    
    12.     lValue = CLng(Mid$(txtScript.Text, lposequalsign + 1, lposEndLine - lposequalsign)) 'Get your Value
    13.  
    14.     lNewValue = lValue * MultiValue
    15.    
    16.     txtScript.Text = Replace(txtScript.Text, pvarName & "=" & CStr(lValue), pvarName & "=" & CStr(lNewValue))
    17.  
    18. End Sub

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Replacing Text

    The RTB has the .Find method that will return the position, as well as select the string. You could also loop thru finding the "=" if those are the only things being changed. Are the variables that you search all the same length?

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    8

    Re: Replacing Text

    Quote Originally Posted by dglienna
    The RTB has the .Find method that will return the position, as well as select the string. You could also loop thru finding the "=" if those are the only things being changed. Are the variables that you search all the same length?
    No they varry but all hold the same name. eg.

    var=5746
    var=46252
    var=6439
    etc

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Replacing Text

    Just find var, then start from there and find "=", then find the next space.
    It's a little more complicated, but not hard to do. My app reads in a rtf that has fields that were saved as [1], [2] ... [45]. These fields were saved in the right columns in 3 different tables that I made in Word. I move thru looking for each number (but you could look for "[" then the first space afterwards.

    VB Code:
    1. With frmReceipt.rtbReceipt
    2.       .Find ("[1]")
    3.       If z4 > 0 Then
    4.         .SelText = Space(6 - Len(Format(z4, "##,##0"))) & Format(z4, "##,##0")
    5.       Else
    6.         .SelText = Space(6)
    7.       End If

    I then format each field, bold and colorize a few (as they are the selected item at the time. Then I go back and center a few lines at the top and bottom, and then paste an image into the picturebox that I made with Word.
    It works great!

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