Consulting

Results 1 to 8 of 8

Thread: Modify text string all over .txt document

  1. #1
    VBAX Newbie
    Joined
    Jul 2018
    Posts
    3
    Location

    Modify text string all over .txt document

    Hi everybody. I am new at this so I'm struggling a bit in changing a repeated string throughout a text file.


    I'd like to replace this kind of string (repeated through the text, where "myVar", myWord and myNumber take different values):


    myFunction("myVar",myWord,30,myNumber)


    for this one:


    myFunction("myVar",myNumber,30,myWord)



    So I simply changed the order for the myFunction input parameters.



    My questions are:
    1) How do I loop through a text document and find each instance of the first string?
    2) How do I modify that string?


    I was thinking in a loop through the file searching for the string "myFunc(", and when this is found, get the text between the two first commas and replace it by the text between the last comma and the last bracket, but I don't know how to do it...


    If anyone has some idea I'd appreciate it.


    Thanks!!

  2. #2
    VBAX Expert
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    543
    Location
    The below will loop through a text file and replace values.
    If you are swapping values then you may have to temporarily call the firsrt swap something different then swap it back (if that makes sense)
        Sub ReplaceStringInFile()
    
        Dim sBuf As String
        Dim sTemp As String
        Dim iFileNum As Integer
        Dim sFileName As String
        
        sFileName = "C:\Users\A\Desktop\test.txt"
        
        iFileNum = FreeFile
        Open sFileName For Input As iFileNum
        
        Do Until EOF(iFileNum)
            Line Input #iFileNum, sBuf
            sTemp = sTemp & sBuf & vbCrLf
        Loop
        Close iFileNum
        
        sTemp = Replace(sTemp, "WordToFind", "ReplacementWord")
        
        iFileNum = FreeFile
        Open sFileName For Output As iFileNum
        Print #iFileNum, sTemp
        Close iFileNum
    
    
    End Sub
    Hope this heps
    I was not told it was impossible, so i did it.

  3. #3
    VBAX Newbie
    Joined
    Jul 2018
    Posts
    3
    Location
    Hello georgiboy. Thanks or that, it was helpful. However, I do not get the swapping part. The problem is that "WordToFind" and "ReplacementWord" are not constant.


    For instance, I'll have to replace lines like the following:


    constantFunction("variableArg_1", variableNoun_1, consant, variableNumber_1) will become ==> constantFunction("variableArg_1", variableNumber_1, consant, variableNoun_1)

    constantFunction("variableArg_2", variableNoun_2, consant, variableNumber_2) will become ==> constantFunction("variableArg_2", variableNumber_2, consant, variableNoun_2)


    So I cannot just replace one word for another. That is why I need to construct one string chain from another by swapping one Function argument for the other (which will be different in each instance).


    I may be asking for too much, but the fact is that I don't know if this is a tedious issue or not, so thanks again for what you said before!!

  4. #4
    VBAX Expert
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    543
    Location
    If this was a one off then I would do the following:

    Change "variableNoun_1" to the word "temp1"
    Then change "variableNumber_1" to "variableNoun_1"
    Then change "temp1" to "variableNumber_1"

    That's what I meant

    Hope this makes sense
    I was not told it was impossible, so i did it.

  5. #5
    VBAX Expert
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    543
    Location
    Ahh I see what you mean now, this could probably be done with use of search and split if you want to rearrange the order of syntax in the string.
    I will have a look but I don't have access to a PC until tomorrow.
    It should make more sense now if someone else looks at it.
    I was not told it was impossible, so i did it.

  6. #6
    VBAX Expert
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    543
    Location
    How about something like this:
    Sub ReplaceStringInFile()
    
        Dim sBuf As String, sTemp As String
        Dim iFileNum As Long, sFileName As String
        Dim oB As Long, cB As Long
        Dim leftPart As String, rightPart As String
        Dim midPart As String, midVar As Variant
        Dim newStr As String
        
        sFileName = "C:\Users\A\Desktop\test.txt"
        
        iFileNum = FreeFile
        Open sFileName For Input As iFileNum
        
        Do Until EOF(iFileNum)
            Line Input #iFileNum, sBuf
            
            If sBuf <> "" Then
                oB = Application.Search("(", sBuf, 1)
                cB = Application.Search(")", sBuf, 1)
                leftPart = Left(sBuf, oB)
                rightPart = Right(sBuf, Len(sBuf) - cB + 1)
                midPart = Mid(sBuf, oB + 1, (cB - 1) - oB)
                midVar = Split(midPart, ",")
                newStr = leftPart & midVar(0) & "," & midVar(3) & "," & midVar(2) & "," & midVar(1) & rightPart
                sTemp = sTemp & newStr & vbCrLf
            End If
        Loop
        
        Close iFileNum
        
        iFileNum = FreeFile
        Open sFileName For Output As iFileNum
        Print #iFileNum, sTemp
        Close iFileNum
        
    End Sub
    Hope this helps
    I was not told it was impossible, so i did it.

  7. #7
    Knowledge Base Approver VBAX Guru
    Joined
    Apr 2012
    Posts
    4,375
    Sub M_snb()
      with createobject("scripting.filesystemobject")
        .createtextfile("C:\example_001.txt").write replace(replace(.opentextfile("C:\example.txt").readall,"aaa","bbb"),"ccc","ddd")
      end with
    Edn Sub

  8. #8
    VBAX Newbie
    Joined
    Jul 2018
    Posts
    3
    Location
    Hi again. Thank you very much!!...I've used your code with a small modification and it worked perfectly
    I attach you what I changed for your information:

    Dim testScriptFile As Long
    Dim testLine As Integer
    Dim strLine As String
    Dim testScriptContent As String
    Dim sBuf As String
    Dim sTemp As String
    Dim oB As Long, cB As Long
    Dim leftPart As String, rightPart As String
    Dim midPart As String, midVar As Variant
    Dim newStr As String
    Dim isRamp As Integer
    
    testScriptFile = FreeFile
    Open testScriptPath For Input As testScriptFile
    Do Until EOF(testScriptFile)
        Line Input #testScriptFile, sBuf            
        isRamp = InStr(sBuf, "Ramp(")               ' I only want to change a specific command, so first of all I search for it the current line
        If isRamp <> 0 Then                         ' If that commmand is contained in sBuf, proceed to change
            sBuf = Replace(sBuf, "%", "")
            oB = Application.Search("(", sBuf, 1)   
            cB = Application.Search(")", sBuf, 1)
            leftPart = Left(sBuf, oB)               
            rightPart = Right(sBuf, Len(sBuf) - cB + 1)
            midPart = Mid(sBuf, oB + 1, (cB - 1) - oB)
            midVar = Split(midPart, ",")
            newStr = leftPart & midVar(0) & "," & midVar(2) & "," & midVar(3) & "," & midVar(1) & rightPart  ' I changed the order of the parts
            sTemp = sTemp & newStr & vbCrLf         
        Else     ' If the line does not contain the string I want to change, save the previous text
            newStr = sBuf
            sTemp = sTemp & newStr & vbCrLf
        End If
    Loop
    Close testScriptFile

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
  •