0

Is there 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 document:

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

This is a text file from which I want to take the values:

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 document will turn into this:

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

1 Answers1

1

Yes, this is possible in AutoIT.

I would read in the second file (car,cat...) using _FileReadToArray(). Then use the function _ReplaceStringInFile().

The below code should get you started in the right direction. FYI: this is not the most efficient code since it opens the file several times.

    $replacementStrings = _FileReadToArray($fileName2)
    $ctr = 0
    While ($ctr < UBound($replacementStrings))
      _ReplaceStringInFile($fileName1,"something",$replacementStrings[$ctr],0,0) 
      ;last 0 says only to replaced the first occurrence.
      $ctr+=1
    Wend

FYI: if "something" appears in other spots in the main file, you will need to write this differently.

Trevor
  • 96
  • 4