Jump to content

TextFile_StringReplace


ripdad
 Share

Recommended Posts

I know there are others like this on the forum - here's my version.

#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
EndFunc

Edit

Updated StringRegExp for case insensitivity and group match.

Edited 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

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

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

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