Sign in to follow this  
Followers 0
myspacee

Search text i a file

13 posts in this topic

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.

Share this post


Link to post
Share on other sites

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.

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.

Share this post


Link to post
Share on other sites

interested on boolean way (true=found, false=not found)

_____________ myfile.txt

cat

dog

fish

$retval = search_text("myfile.txt","cat")

---------> $retval = true

m.

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 by someone

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd

Share this post


Link to post
Share on other sites

$file = FileRead("sometext.txt")
If StringInStr($file, "search") Then
;Found
Else
;Not found
EndIf

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#10 ·  Posted (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 by Pain

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

This is one of many ways of replacing specific lines in a file.

#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

Share this post


Link to post
Share on other sites

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

:)

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  
Followers 0