Jump to content
Sign in to follow this  
samkshah

_ReplaceStringInFile - replace string based on partial match

Recommended Posts

samkshah

Hello,

I appreciate any help on this.

I need to replace a line in a file, however the complete line is unknown, only starting characters are known.

Any idea on how to replace the entire line based on if starting word matches my search criteria? Here is a sample code and the output that I need.

#include <File.au3>

$retval = 0

$find = "BEFORE*";line starting with "BEFORE"  - rest of words in that string are unknown. Not sure if I can use "*" to wildcard everything after BEFORE
$replace = "AFTER"

$filename = "C:\test.txt"

$retval = _ReplaceStringInFile($filename,$find,$replace)
if $retval = -1 then
    msgbox(0, "ERROR", "The pattern could not be replaced in file: " & $filename & " Error: " & @error)
    exit
else
    msgbox(0, "INFO", "Found " & $retval & " occurances of the pattern: " & $find & " in the file: " & $filename)
endif

Share this post


Link to post
Share on other sites
herewasplato

Read the entire file into one variable using FileRead.

Use StringSplit to split that variable into an array.

Loop thru the array and use StringTrimLeft to find the line(s) of interest.

If found - do what you want to that line...


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites
samkshah

herewasplato,

Thanks.

Let me try that and will update you once done.

Appreciate your quick help.

samkshah

Share this post


Link to post
Share on other sites
samkshah

I always get Error 6 with above code. Am I missing anything? I checked file attributes and it is not a read only file.

Thanks.

Share this post


Link to post
Share on other sites
herewasplato

Can you post the exact code that is giving this error?


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites
PsaltyDS

This worked:

#include <file.au3>

$find = "BEFORE"
$replace = "AFTER"
$filename = "C:\Temp\Test.txt"

Dim $avLines
If _FileReadToArray($filename, $avLines) And $avLines[0] > 0 Then
    $Count = 0
    For $n = 1 To $avLines[0]
        If StringLeft($avLines[$n], StringLen($find)) = $find Then
            $Count += 1
            $avLines[$n] = $replace & StringTrimLeft($avLines[$n], StringLen($find))
        EndIf
    Next
    If Not $Count Then
        MsgBox(16, "Error", "No instances of '" & $find & "' found!", 10)
        Exit
    EndIf
    
    If _FileWriteFromArray($filename, $avLines, 1) Then; Start from index=1 to not write count in [0]
        MsgBox(64, "Finished", "Successfully replaced " & $Count & " instances of '" & $find & "' with '" & $replace & "'")
    Else
        MsgBox(16, "Error", "Error occured writing array to file: " & $filename, 10)
    EndIf
Else
    MsgBox(16, "Error", "Error reading file into array.")
EndIf

Using this for a Test.txt file:

Line 1.
This is a test.
Line 3. 
Before the rest of the line.
Line 5.
The middle of the line is before the end.
Line 7.

Note that the search is not case sensitive. Change '=' to '==' for case sensitive matching. And this code ONLY replaces a max of one instance per line, only at the beginning.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  

×