JakeAI Posted April 15, 2013 Share Posted April 15, 2013 Hi! 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! Link to comment Share on other sites More sharing options...
JohnOne Posted April 15, 2013 Share Posted April 15, 2013 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. Link to comment Share on other sites More sharing options...
JakeAI Posted April 15, 2013 Author Share Posted April 15, 2013 Thank you for your fast reply! I am now trying to write the script. Link to comment Share on other sites More sharing options...
JohnOne Posted April 15, 2013 Share Posted April 15, 2013 Great stuff. Remember to post any code you try, if stuck on something. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
JakeAI Posted April 15, 2013 Author Share Posted April 15, 2013 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. Link to comment Share on other sites More sharing options...
JohnOne Posted April 15, 2013 Share Posted April 15, 2013 First, just open, run and try to see what is going on in the StringReplace Example in the help file. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
AZJIO Posted April 15, 2013 Share Posted April 15, 2013 #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) My other projects or all Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now