# Function Reference

# _ReplaceStringInFile

Replaces substrings in a file

#include <File.au3>
_ReplaceStringInFile ( $sFilePath, $sSearchString, $sReplaceString [, $iCaseSensitive = 0 [, $iOccurance = 1]] )

## Parameters

$sFilePath | Full path of file to replace substrings.
$sSearchString | The string to evaluate.
$sReplaceString | The replacement string.
$iCaseSensitive | [optional] Flag to indicate if the operations should be case sensitive. $STR_NOCASESENSE (0) = not case sensitive, using the user's locale (default) $STR_CASESENSE (1) = case sensitive $STR_NOCASESENSEBASIC (2) = not case sensitive, using a basic/faster comparison Constants are defined in StringConstants.au3
$iOccurance | [optional] 0 - Only the first occurrence is replaced or 1 - all occurrences are replaced (default)

## Return Value

Success: | the number of occurrences found.
Failure: | -1 and sets the @error flag to non-zero.
@error: | 1 - File is read-only 2 - Unable to open the file 3 - Unable to write to the file

## Example

    #include <File.au3>
    #include <MsgBoxConstants.au3>

    Local $sFind = "BEFORE"
    Local $sReplace = "AFTER"

    Local $sFileName = "C:\_ReplaceStringInFile.test"

    Local $iMsg = "Hello Test " & $sFind & " Hello Test" & @CRLF
    $iMsg &= "Hello Test" & @CRLF
    $iMsg &= @CRLF
    $iMsg &= $sFind

    FileWrite($sFileName, $iMsg)

    MsgBox($MB_SYSTEMMODAL, "BEFORE", $iMsg)

    Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)
    If $iRetval = -1 Then
        MsgBox($MB_SYSTEMMODAL, "ERROR", "The pattern could not be replaced in file: " & $sFileName & " Error: " & @error)
        Exit
    Else
        MsgBox($MB_SYSTEMMODAL, "INFO", "Found " & $iRetval & " occurances of the pattern: " & $sFind & " in the file: " & $sFileName)
    EndIf

    $iMsg = FileRead($sFileName, 1000)
    MsgBox($MB_SYSTEMMODAL, "AFTER", $iMsg)
    FileDelete($sFileName)