Jump to content

ReplaceStringInFiles


jvanegmond
 Share

Recommended Posts

Larry wrote _FileSearch and _FileSearchUtil. This is an example showing how to replace all black colors, with an orangy one in all htm and html files.

ReplaceStringInFiles("*.htm*", "#000000", "#FF6600")

Func ReplaceStringInFiles($Filemask, $Searchstring, $Replacestring)
    $a = _FileSearch($Filemask,1)
    If $a[0] > 0 Then
        For $i = 1 to $a[0]
            $File = $a[$i]
            $Read = FileRead($File)
            FileDelete($File)
            $Read = StringReplace($Read,$Searchstring, $Replacestring)
            FileWrite($File,$Read)
        Next
    EndIf
EndIf

Func _FileSearch($szMask,$nOption) ; by Larry!
    $szRoot = ""
    $hFile = 0
    $szBuffer = ""
    $szReturn = ""
    $szPathList = "*"
    Dim $aNULL[1]

    If Not StringInStr($szMask,"\") Then
         $szRoot = @SCRIPTDIR & "\"
    Else
         While StringInStr($szMask,"\")
              $szRoot = $szRoot & StringLeft($szMask,StringInStr($szMask,"\"))
              $szMask = StringTrimLeft($szMask,StringInStr($szMask,"\"))
         Wend
    EndIf
    If $nOption = 0 Then
         _FileSearchUtil($szRoot, $szMask, $szReturn)
    Else
         While 1
              $hFile = FileFindFirstFile($szRoot & "*.*")
              If $hFile >= 0 Then
                   $szBuffer = FileFindNextFile($hFile)
                   While Not @ERROR
                        If $szBuffer <> "." And $szBuffer <> ".." And _
                             StringInStr(FileGetAttrib($szRoot & $szBuffer),"D") Then _
                             $szPathList = $szPathList & $szRoot & $szBuffer & "*"
                        $szBuffer = FileFindNextFile($hFile)
                   Wend
                   FileClose($hFile)
              EndIf
              _FileSearchUtil($szRoot, $szMask, $szReturn)
              If $szPathList == "*" Then ExitLoop
              $szPathList = StringTrimLeft($szPathList,1)
              $szRoot = StringLeft($szPathList,StringInStr($szPathList,"*")-1) & "\"
              $szPathList = StringTrimLeft($szPathList,StringInStr($szPathList,"*")-1)
         Wend
    EndIf
    If $szReturn = "" Then
         $aNULL[0] = 0
         Return $aNULL
    Else
         Return StringSplit(StringTrimRight($szReturn,1),"*")
    EndIf
EndFunc

Func _FileSearchUtil(ByRef $ROOT, ByRef $MASK, ByRef $RETURN) ; by Larry!
    $hFile = FileFindFirstFile($ROOT & $MASK)
    If $hFile >= 0 Then
         $szBuffer = FileFindNextFile($hFile)
         While Not @ERROR
              If $szBuffer <> "." And $szBuffer <> ".." Then _
                   $RETURN = $RETURN & $ROOT & $szBuffer & "*"
              $szBuffer = FileFindNextFile($hFile)
         Wend
         FileClose($hFile)
    EndIf
EndFunc
Link to comment
Share on other sites

Larry wrote _FileSearch and _FileSearchUtil. This is an example showing how to replace all black colors, with an orangy one in all htm and html files.

be careful with the naming. There is already a function _ReplaceStringInFile(), which is part of the standard UDFs.

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Yes, thank you. I just want to share this because I found this incredibly useful, while I was building a website, and had to change 200 files. I do not plan to get this included in any of the standard UDF's.

ah, then you might want to take a look at this as well: http://www.autoitscript.com/forum/index.php?showtopic=40542. A simple ReplaceString function would look like this:

#include <File.au3>
Func _ReplaceStrings($filepath,$searchstring,$replacestring)
      _ReplaceStringInFile($filepath,$searchstring,$replacestring)
Endfunc

No error checking done!

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

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