Jump to content
Sign in to follow this  
JakeAI

Replace text with multiple unique values each time

Recommended Posts

JakeAI

Hi! :bye: I am a new member of this community and also a new AutoIt user. I know this is a powerful tool and I am wondering if there is any way to replace text that appears several times in a .txt file with a unique value each time. To be more specific:

This is the given test.txt document:

value="something"
other text lines
value="something"
other text lines
...

This is a text file from which I want to take the values(values.txt for example):

car
cat
...

What I want is to automatically replace "something" with a different value each time taken from the other text file, so the given test.txt document will turn into this:

value="car"
other text lines
value="cat"
other text lines
...

Thank you! :)

Share this post


Link to post
Share on other sites
JohnOne

StringInStr() to get the number of occurrences of target word.

Repeated calls to StringReplace() using data returned from above function, while looping through an array of replacement words.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
JakeAI

Thank you for your fast reply! I am now trying to write the script. :thumbsup:

Share this post


Link to post
Share on other sites
JakeAI

I'm afraid I don't really know where to begin. Do I have to import those 2 text files? Will it be easier if I already know how many times "something" occurs in the test.txt document? I would like to make the script extract the first line from values.txt and use it to replace the first "something" that occurs in test.txt and then replace the second "something" that occurs in test.txt with the second line from values.txt and so on.

I am really sorry but I am an absolute beginner. :oops:

Share this post


Link to post
Share on other sites
AZJIO

#include <Array.au3> ; для _ArrayDisplay
#include <FileOperations.au3>

$sPath = @ScriptDir & '\file'

$aFileList = _FO_FileSearch($sPath, '*.txt')

$iExtended = 0
$iFiles = 0
$iCount = 0

For $i = 1 To $aFileList[0]
    $iCount = 0
    $sText = FileRead($aFileList[$i])

    $sText = StringReplace($sText, 'something', 'car')
    If Not @error Then
        $iExtended += @extended
        $iCount += 1
    EndIf

    $sText = StringRegExpReplace($sText, 'something|other|text|lines', 'string')
    If Not @error Then
        $iExtended += @extended
        $iCount += 1
    EndIf

    If $iCount Then
        $iFiles += 1
        $hFile = FileOpen($aFileList[$i], 2)
        FileWrite($hFile, $sText)
        FileClose($hFile)
    EndIf
Next

MsgBox(0, 'Done', 'Replace= ' & $iExtended & ', Files= ' & $iFiles)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×