Jump to content

_FileReplaceText


Recommended Posts

Well, I searched but couldn't find this function nor anything quite exactly what I wanted. Please feel free to point me in the right direction if it's previously been done. Apologies for wrapping lines which you'll probably need to correct after copy/paste.

; #FUNCTION# =====================================================================================================================
; Name...........: _FileReplaceText
; Description ...: Replaces text in a file.
; Syntax.........: _FileReplaceText($sFile, $sSearchText, $sReplaceText[, $fRegExp = 0[, $fReplaceLine = 0[, $fWriteBlanks = 1]]])
; Parameters ....: $sFile        - The file to write to
;                  $sSearchText  - The text to search for
;                  $sReplaceText - The text to replace
;                  $fRegExp      - If set to 0 will use StringReplace
;                  |If set to 1 will use StringRegExpReplace
;                  $fReplaceLine - If set to 0 will not replace the entire line with $sReplaceText
;                  |If set to 1 will replace the entire line with $sReplaceText
;                  $fWriteBlanks - If set to 0 will write blank lines to the file
;                  |If set to 1 will suppress blank lines from the file
; Return values .: Success - 1
;                  Failure - 0
;                  @Error  - 0 = No error
;                  |1 = File does not exist
;                  |2 = Error when opening file
;                  |3 = $fRegExp is invalid
;                  |4 = $fWriteBlanks is invalid
;                  |5 = $fReplaceLine is invalid
;                  |6 = $sSearchText is invalid
;                  |7 = $sReplaceText is invalid
; Author ........: WideBoyDixon
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; No
; ================================================================================================================================
Func _FileReplaceText($sFile, $sSearchText, $sReplaceText, $fRegExp = 0, $fReplaceLine = 0, $fWriteBlanks = 1)
    If Not FileExists($sFile) Then Return SetError(1, 0, 0)
    If $fRegExp <> 0 And $fRegExp <> 1 Then Return SetError(3, 0, 0)
    If $fWriteBlanks <> 0 And $fWriteBlanks <> 1 Then Return SetError(4, 0, 0)
    If $fReplaceLine <> 0 And $fReplaceLine <> 1 Then Return SetError(5, 0, 0)
    If Not IsString($sSearchText) Then Return SetError(6, 0, 0)
    If Not IsString($sReplaceText) Then Return SetError(7, 0, 0)
    Local $filtxt = FileRead($sFile, FileGetSize($sFile))
    $filtxt = StringSplit($filtxt, @CRLF, 1)
    Local $fil = FileOpen($sFile, 2)
    If $fil = -1 Then Return SetError(2, 0, 0)
    Local $sLine
    For $i = 1 To $filtxt[0]
        If $fRegExp = 0 Then
            If $fReplaceLine = 0 Then
                $sLine = StringReplace($filtxt[$i], $sSearchText, $sReplaceText)
                If @error Then $sLine = $filtxt[$i]
            Else
                If StringInStr($filtxt[$i], $sSearchText) Then
                    $sLine = $sReplaceText
                Else
                    $sLine = $filtxt[$i]
                EndIf
            EndIF
        Else
            If $fReplaceLine = 0 Then
                $sLine = StringRegExpReplace($filtxt[$i], $sSearchText, $sReplaceText)
                If @error Then $sLine = $filtxt[$i]
            Else
                If StringRegExp($filtxt[$i], $sSearchText) Then
                    $sLine = $sReplaceText
                Else
                    $sLine = $filtxt[$i]
                EndIf
            EndIf
        EndIf
        If $fWriteBlanks = 1 Or $sLine <> "" Then FileWrite($fil, $sLine & @CRLF)
    Next
    FileClose($fil)
    Return 1
EndFunc   ;==>_FileReplaceTextoÝ÷ ØLZ^~º&²¶§²Z()àj×®X­«­¢+Ù}¥±IÁ±QáÐ ÅÕ½ÐíèÀäÈíAɽɴ¥±ÌÀäÈíÕѽ%ÐÌÀäÈíM¥QÀäÈíAɽÁÉÑ¥ÌÀäÈíÔ̹ÁɽÁÉÑ¥ÌÅÕ½Ðì°ÅÕ½ÐíÕѽ¥ÐÍ¥ÈôÅÕ½Ðì°ÅÕ½ÐíÕѽ¥ÐÍ¥ÈõèÀäÈíQµÀÀäÈíÕѽ%ÐÌÅÕ½Ðì°À°Ä

WBD

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...