ripdad Posted December 24, 2010 Share Posted December 24, 2010 (edited) I know there are others like this on the forum - here's my version.expandcollapse popup#include <file.au3> ;============================================================================================= ; TextFile_StringReplace ; Released: December 24, 2010 by ripdad ; ; Description: Replaces a string in a "plain text file" ; File Types: .ini, .txt, .log, etc. ; Example: Yes ; ; TextFile_StringReplace(@HomeDrive & '\somefile.txt', 'some string', 'some other string', 1) ; ; #vars defined# ; ; $sFile = path and filename ; $sString = search for this string ; $rString = replace with this string ; $fAction = action for current file before string replace ; 1 = make back-up (default) ; 2 = recycle ; 3 = delete ;============================================================================================= Func TextFile_StringReplace($sFile, $sString = '', $rString = '', $fAction = 1) If Not FileExists($sFile) Then Return SetError(-1) Local $attrib = FileGetAttrib($sFile); get original attributes If StringInStr($attrib, 'D') Then Return SetError(-2); can't process a folder If Not StringRegExp(StringRight($sFile, 3), '(?i)(ini)|(txt)|(log)') Then Return SetError(-3); file ext control If $sString = '' Then Return SetError(-4); no point in a blank search string If $attrib Then; remove attributes If Not FileSetAttrib($sFile, '-' & $attrib) Then Return SetError(-5) EndIf Local $array, $dt _FileReadToArray($sFile, $array) If @error Then Return SetError(-6) If $fAction = 1 Then $dt = @MON & '-' & @MDAY & '-' & @YEAR & '-' & @HOUR & @MIN & @SEC & '.' & @MSEC If Not FileMove($sFile, $sFile & '.' & $dt & '.bak') Then Return SetError(-7) ElseIf $fAction = 2 Then If Not FileRecycle($sFile) Then Return SetError(-7) ElseIf $fAction = 3 Then If Not FileDelete($sFile) Then Return SetError(-7) Else Return SetError(-8); not a valid action EndIf For $i = 1 To $array[0] If StringInStr($array[$i], $sString) Then $array[$i] = StringReplace($array[$i], $sString, $rString) EndIf FileWriteLine($sFile, $array[$i]) Next If $attrib Then FileSetAttrib($sFile, '+' & $attrib); set to original attributes EndFuncEditUpdated StringRegExp for case insensitivity and group match. Edited December 25, 2010 by ripdad "The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward Link to comment Share on other sites More sharing options...
mesale0077 Posted December 24, 2010 Share Posted December 24, 2010 thank you ripdad very cool script Link to comment Share on other sites More sharing options...
mesale0077 Posted December 24, 2010 Share Posted December 24, 2010 hi doc file or xls DocFile_StringReplace( ) and XlsFile_StringReplace( ) you think ? thank you Link to comment Share on other sites More sharing options...
ripdad Posted December 24, 2010 Author Share Posted December 24, 2010 mesale0077, .doc and .xls files are over my head at the moment, since I don't use them at all. No promises for one -- but ... I'll look into it. And you're welcome! "The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward Link to comment Share on other sites More sharing options...
ripdad Posted December 25, 2010 Author Share Posted December 25, 2010 I've taken a quick look at doc v6 and doc v8 files. These "appear editable" .. so long as it's not encrypted. .xls is definitely out of the question. Just being curious .. why would anyone want to edit these filetypes outside their native program? "The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now