Jump to content



Photo

_FileReplaceText


  • Please log in to reply
1 reply to this topic

#1 WideBoyDixon

WideBoyDixon

    Code Monkey

  • Active Members
  • PipPipPipPipPipPip
  • 381 posts

Posted 06 May 2009 - 10:44 PM

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.

AutoIt         
; #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   ;==>_FileReplaceTextƒo݊÷ ØLZš™^~º&²‰ž¶§€Œ²Z(’)àjך®Xž­«­¢+Ù}¥±•I•Á±…•Q•áÐ ™ÅÕ½Ðí 蘌ÀäÈíAɽÉ…´¥±•̘ŒÀäÈíÕѽ%Ð̘ŒÀäÈíM¥Q˜ŒÀäÈíAɽÁ•ÉÑ¥•̘ŒÀäÈí…Ô̹ÁɽÁ•ÉÑ¥•Ì™ÅÕ½Ð찀™ÅÕ½Ðí…Õѽ¥Ð͑¥Èô™ÅÕ½Ð찀™ÅÕ½Ðí…Õѽ¥Ð͑¥Èõ 蘌ÀäÈíQ•µÀ˜ŒÀäÈíÕѽ%Ð̙ÅÕ½Ð찀À°€Ä


WBD







#2 Authenticity

Authenticity

    Universalist

  • MVPs
  • 2,619 posts

Posted 06 May 2009 - 10:52 PM

Heh, very nice function ^_^.
You can write additional function to escape metacharacter in the replacement string if the user is willing to preform a RegExp search and replace.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users