Jump to content

Find & Replace Text


Recommended Posts

Hi There :think:

I am a newbie to autoit scripting. I have to enter text into a text file on a certain position. I managed to get the script to enter the text into the file.

What i am struggling with is getting it to the right place!

I need some assistance!

Regards

Griffin

Link to comment
Share on other sites

Hi There :(

I am a newbie to autoit scripting. I have to enter text into a text file on a certain position. I managed to get the script to enter the text into the file.

What i am struggling with is getting it to the right place!

I need some assistance!

Regards

Griffin

HI,

what about FileReadToArray() and then check every line with StringInStr() <> 0 Then "found at position is returned". Now you can add the text behind it.

Just a quick thought. :think:

So long,

Mega

Edited by th.meger

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

  • Moderators

HI,

what about FileReadToArray() and then check every line with StringInStr() <> 0 Then "found at position is returned". Now you can add the text behind it.

Just a quick thought. :think:

So long,

Mega

Also keep in mind that _FileReadToArray() is in Beta, and you could use _FileWriteToLine() once the string that your looking for has been found:
Local $nArray = '', $TextToFind = 'Whatever I need to find', $FileLocation = 'Location To File'
_FileReadToArray($FileLocation, $nArray)
For $x = 1 To UBound($nArray) - 1
    If StringInStr($nArray[$x], $TextToFind) Then
        _FileWriteToLine($FileLocation, $x, 'Add This To Line ', 0); 0 adds it, 1 replaces the line with what text you want to replace with
        ExitLoop; Use this if you only want it to replace the first instance it finds of it
    EndIf
Next
Both _FileReadToArray() and _FileWriteToArray() require Beta: AutoIt Beta Here

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

Also keep in mind that _FileReadToArray() is in Beta, and you could use _FileWriteToLine() once the string that your looking for has been found:

Local $nArray = '', $TextToFind = 'Whatever I need to find', $FileLocation = 'Location To File'
_FileReadToArray($FileLocation, $nArray)
For $x = 1 To UBound($nArray) - 1
    If StringInStr($nArray[$x], $TextToFind) Then
        _FileWriteToLine($FileLocation, $x, 'Add This To Line ', 0); 0 adds it, 1 replaces the line with what text you want to replace with
        ExitLoop; Use this if you only want it to replace the first instance it finds of it
    EndIf
Next
Both _FileReadToArray() and _FileWriteToArray() require Beta: AutoIt Beta Here
HI SmOke_N,

everything totally right what you are saying. I just thought of my soultion, because with _FileWriteToLine it could be hard to write text in a given line (like insert).

So long,

Mega

Edited by th.meger

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Something like....

$file = FileOpen("myfile.txt", 0)
$NewFile = FileOpen ( "myfile.tmp", 2 )
$searchString="my search string"
$relacementString="my Replacement String"
; Check if file opened for reading OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

; Read in lines of text until the EOF is reached
    While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop

        If $line = $searchString then
        $Line = $relacementString
        Endif

        FileWriteLine ($NewFile, $Line)
    Wend
    
    FileClose ( $File )
    FileClose ( $NewFile )
    FileDelete ( "myfile.txt" )
    FileMove("myfile.tmp", "myFile.txt")
Link to comment
Share on other sites

HI,

without StringInStr() checking, you ' ll always only be able to replace the hole line in a txt file.

But, nevertheless nice example to start for him.

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

StringRegExpReplace seems like a viable solution as well.

Can you post the sample text file and what you are trying to insert?

AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)
Link to comment
Share on other sites

I say just fileread the entire text file into a variable, stringregexpreplace the desired string into its position globally.

Then write the contents of the variable back to the file, viola.

AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)
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...