Quantcast
Channel: AutoIt v3 - Developer Chat
Viewing all articles
Browse latest Browse all 750

ReplaceStringInFile

$
0
0
AutoIt         
Func _ReplaceStringInFiletmp($sFilePath, $sSearchString, $sReplaceString, $iCaseSensitive = 0, $iOccurance = 1)     If StringInStr(FileGetAttrib($sFilePath), "R") Then Return SetError(1, 0, -1)     ; Open the file for reading.     Local $hFileOpen = FileOpen($sFilePath, $FO_READ)     If $hFileOpen = -1 Then Return SetError(2, 0, -1)     ; Read the contents of the file and stores in a variable     Local $sFileRead = FileRead($hFileOpen)     FileClose($hFileOpen) ; Close the open file after reading     ; Set the default parameters     If $iCaseSensitive = Default Then $iCaseSensitive = 0     If $iOccurance = Default Then $iOccurance = 1     ; Replace strings     $sFileRead = StringReplace($sFileRead, $sSearchString, $sReplaceString, 1 - $iOccurance, $iCaseSensitive)     Local $iReturn = @extended ; At this point $iReturn = 1     ; If there are replacements then overwrite the file     If $iReturn Then         ; Retrieve the file encoding         Local $iFileEncoding = FileGetEncoding($sFilePath)         ; Open the file for writing and set the overwrite flag         $hFileOpen = FileOpen($sFilePath, $iFileEncoding + $FO_OVERWRITE)         If $hFileOpen = -1 Then Return SetError(3, 0, -1)         ; Write to the open file         FileWrite($hFileOpen, $sFileRead)         FileClose($hFileOpen) ; Close the open file after writing     EndIf ; but here $iReturn = 0 making it look like an error     Return $iReturn EndFunc   ;==>_ReplaceStringInFile

I have added two comments to this UDF toward the bottom.  It would seem that using $iReturn when other functions is this issue.  No help needed just wanted to point out the probmem I was having.


Viewing all articles
Browse latest Browse all 750

Latest Images

Trending Articles



Latest Images