myspacee Posted May 29, 2008 Share Posted May 29, 2008 Sorry for this question but can't resolve. use this (ugly code) : $retval = _ReplaceStringInFile("c:\mytext.txt",$sMytext,$sMytext) to obtain $retval, but value is = 0 !! (not in doc) anyone can post clean code to retrieve text in a .txt file ? thank you for help m. Link to comment Share on other sites More sharing options...
weaponx Posted May 29, 2008 Share Posted May 29, 2008 Sorry for this question but can't resolve.use this (ugly code) :$retval = _ReplaceStringInFile("c:\mytext.txt",$sMytext,$sMytext)to obtain $retval, but value is = 0 !! (not in doc)anyone can post clean code to retrieve text in a .txt file ?thank you for helpm.You need to be more specific. Searching for a string retrieves a boolean (true=found, false=not found) or integer result (string position). You say you want to retrieve text though, if you are searching for $Needle in $Haystack and there is a match the only text you will get back is $Needle. Link to comment Share on other sites More sharing options...
myspacee Posted May 29, 2008 Author Share Posted May 29, 2008 interested on boolean way (true=found, false=not found) _____________ myfile.txt cat dog fish $retval = search_text("myfile.txt","cat") ---------> $retval = true m. Link to comment Share on other sites More sharing options...
someone Posted May 29, 2008 Share Posted May 29, 2008 (edited) The only thing that jumps out at me is that your search string is the same as your replace string. Are you sure you want that? Wait are you trying only to count the instances of $sMytext in a file? You could use _FileReadToArray and then StringInStr, or FileReadLine and again StringinStr.... see the example for FileReadLine. Also weaponx I believe your wrong, _ReplaceStringInFile doesn't return the string position, but rather, "Success: Returns the number of occurrences of $szSearchString we found" Haha is it bad I had to read that like 7 times before calling you out? Edited May 29, 2008 by someone While ProcessExists('Andrews bad day.exe') BlockInput(1) SoundPlay('Music.wav') SoundSetWaveVolume('Louder') WEnd Link to comment Share on other sites More sharing options...
weaponx Posted May 29, 2008 Share Posted May 29, 2008 $file = FileRead("sometext.txt") If StringInStr($file, "search") Then ;Found Else ;Not found EndIf Link to comment Share on other sites More sharing options...
weaponx Posted May 29, 2008 Share Posted May 29, 2008 The only thing that jumps out at me is that your search string is the same as your replace string. Are you sure you want that?Wait are you trying only to count the instances of $sMytext in a file? You could use _FileReadToArray and then StringInStr, or FileReadLine and again StringinStr.... see the example for FileReadLine.Also weaponx I believe your wrong, _ReplaceStringInFile doesn't return the string position, but rather, "Success: Returns the number of occurrences of $szSearchString we found"Haha is it bad I had to read that like 7 times before calling you out?Well read it again because I never said anything about _ReplaceStringInFile. I said a string search generally returns a boolean or integer value. Link to comment Share on other sites More sharing options...
someone Posted May 29, 2008 Share Posted May 29, 2008 Well when you reply to a question regarding _ReplaceStringInFile, but reference other functions' return values (generally or specifically) and don't make a point to say that... it could be a bit confusing. While ProcessExists('Andrews bad day.exe') BlockInput(1) SoundPlay('Music.wav') SoundSetWaveVolume('Louder') WEnd Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted May 29, 2008 Moderators Share Posted May 29, 2008 Wait are you trying only to count the instances of $sMytext in a file? You could use _FileReadToArray and then StringInStr, or FileReadLine and again StringinStr.... see the example for FileReadLine.A much much faster method to do this would be: Local $sMyStr = FileRead("MyFile.ext") Local $sMySearch = "FindThis" StringReplace($sMystr, $sMySearch, "") Local $nNumOfOccurrences = @extended MsgBox(64, "Info", "Number of occurrences in the file: " & $nNumOfOccurrences) Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
myspacee Posted December 30, 2008 Author Share Posted December 30, 2008 sorry for take again this post. this method help me in past to understard if a word is or not in a file. Now i need to search text in a file, and if i found, replace entire row, with my text string. anyone can suggest me how? thank you, m. Link to comment Share on other sites More sharing options...
Pain Posted December 30, 2008 Share Posted December 30, 2008 (edited) $file = FileOpen("test.txt", 0) $text = "" While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop If StringInStr($line, "hello world") Then $text &= "new row" &@CRLF Else $text &= $line &@CRLF EndIf Wend FileClose($file) ConsoleWrite($text) EDIT: Working code. Edited December 31, 2008 by Pain Link to comment Share on other sites More sharing options...
myspacee Posted December 30, 2008 Author Share Posted December 30, 2008 Pain thank you for code, but it searches for text line x line and when found do something... maybe with your code, script can count row where text is, using _ReplaceStringInFile is possible to solve isssue i think... too late also for me, for today i quit (i've sort of deejavu) good night all, m. Link to comment Share on other sites More sharing options...
Malkey Posted December 31, 2008 Share Posted December 31, 2008 This is one of many ways of replacing specific lines in a file. expandcollapse popup#include <Array.au3> #include <File.au3> Local $sFile = "Charge1.txt"; Full path name of file unless in script directory. Local $sSearchFor = "league";Search for this text within a line to be replaced Local $sNewline = "The Shaddow knows the square root of pie,"; Replace found lines with this line _FileReplaceLine($sFile, $sSearchFor, $sNewline) ;=========== _FileReplaceLine ===================== ; Description: Reads a file and replaces the lines which has specified text in it with a specified replacement line. ; Parameters : $sFile - The full path name of the file. ; $sSearchFor - (string) The text to search for to identify a line to be replaced. ; $sNewline - (string) The new replacement line to be used. ;================================================= ; Func _FileReplaceLine($sFile, $sSearchFor, $sNewline) Local $iStart = 0, $info = "" Local $aResult, $idButt _FileReadToArray($sFile, $aResult) _ArrayDisplay($aResult) Do $iIndex = _ArraySearch($aResult, $sSearchFor, $iStart, 0, 1, 1) Local $err = @error If $err = 6 Then If $info = "" Then MsgBox(0, "", "String not found") Else $info &= "Replaced line " & $iIndex & @CRLF $aResult[$iIndex] = $sNewline If ($iIndex) >= UBound($aResult) - 1 Then; Check if on last line then exit $err = 6 Else $iStart = $iIndex +1 EndIf EndIf Until $err = 6 If $info <> "" Then $idButt = MsgBox(1, "Press OK to change file", $info) If $idButt = 1 Then _FileWriteFromArray($sFile, $aResult, 1) EndIf EndFunc ;==>_FileReplaceLine Link to comment Share on other sites More sharing options...
rasim Posted December 31, 2008 Share Posted December 31, 2008 More examples: _FileReplaceLine(@ScriptDir & "\Test.txt", "Search String", "Replacement string", 1) Func _FileReplaceLine($sFile, $sSearch, $sReplace, $iCount = 0) If FileGetSize($sFile) = 0 Then Return SetError(1, 0, 0) Local $sRead, $aStrings, $sResult, $sFound = 0, $hFile, $iTotalCount = 0, $i $sRead = FileRead($sFile) $aStrings = StringSplit($sRead, @CRLF, 1) For $i = 1 To $aStrings[0] If StringInStr($aStrings[$i], $sSearch) Then If ($iCount = 0) Or ($iTotalCount < $iCount) Then $iTotalCount += 1 $sFound &= $i & ";" $sResult &= $sReplace & @CRLF ContinueLoop EndIf EndIf $sResult &= $aStrings[$i] & @CRLF Next $hFile = FileOpen($sFile, 2) FileWrite($hFile, $sResult) FileClose($hFile) Return $sFound EndFunc ;==>_FileReplaceLine 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