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
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,192
    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
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  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
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,192
    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
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  5. #5
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,192
    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.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  6. #6
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,192
    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
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    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
  •