Jump to content

Search text i a file


Recommended Posts

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

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.

Link to comment
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?

Edited by someone
While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
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.

Link to comment
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
Link to comment
Share on other sites

  • Moderators

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

  • 7 months later...

$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
Link to comment
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.

Link to comment
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
Link to comment
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

:)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...