Jump to content

How do I replace a character in a string in an ANSI encoded file?


Recommended Posts

Create copy of _FileWriteToLine() from "C:\Program Files (x86)\AutoIt3\Include\File.au3" 

and modify FileOpen(...,2) to FileOpen(...,2+512) 

$FO_OVERWRITE (2) + $FO_ANSI (512)

#include <File.au3>

$hWrite = FileOpen("1.txt", 512);
$sLine20 = FileReadLine($hWrite, 2); 
$sText = StringReplace($sLine20, 5, "W")
_FileWriteToLineAnsi('1.txt', 2, $sText, 1);
FileClose($hWrite)

Func _FileWriteToLineAnsi($sFile, $iLine, $sText, $fOverWrite = 0)
    If $iLine <= 0 Then Return SetError(4, 0, 0)
    If Not IsString($sText) Then Return SetError(6, 0, 0)
    If $fOverWrite <> 0 And $fOverWrite <> 1 Then Return SetError(5, 0, 0)
    If Not FileExists($sFile) Then Return SetError(2, 0, 0)
    
    Local $filtxt = FileRead($sFile, FileGetSize($sFile))
    $filtxt = StringSplit($filtxt, @CRLF, 1)
    If UBound($filtxt, 1) < $iLine Then Return SetError(1, 0, 0)
    Local $fil = FileOpen($sFile, 2+512) ; --> modified by Zedna: $FO_OVERWRITE (2) + $FO_ANSI (512)
    If $fil = -1 Then Return SetError(3, 0, 0)
    For $i = 1 To UBound($filtxt) - 1
        If $i = $iLine Then
            If $fOverWrite = 1 Then
                If $sText <> '' Then
                    FileWrite($fil, $sText & @CRLF)
                Else
                    FileWrite($fil, $sText)
                EndIf
            EndIf
            If $fOverWrite = 0 Then
                FileWrite($fil, $sText & @CRLF)
                FileWrite($fil, $filtxt[$i] & @CRLF)
            EndIf
        ElseIf $i < UBound($filtxt, 1) - 1 Then
            FileWrite($fil, $filtxt[$i] & @CRLF)
        ElseIf $i = UBound($filtxt, 1) - 1 Then
            FileWrite($fil, $filtxt[$i])
        EndIf
    Next
    FileClose($fil)
    Return 1
EndFunc   ;==>_FileWriteToLineAnsi

Note: this my function is copy from old AutoIt 3.2.12.1 which i my main version!

So make your own modified copy from your actual version of AutoIt ...

 

Edited by Zedna
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...