Jump to content

powful search or _ReplaceStringInFile udf function


yidabu
 Share

Recommended Posts

code here:

;myReplaceStringInFile:
;
;English Version:
;Example:
;myReplaceStringInFile("D:\AutoIt\复件 article", "discuz", "BackUp,_private,images", "(?m)^tag:", "thisistag:")
; ===============================================================================
;
; Function Name:   myReplaceStringInFile
; Description::    find or Replaces a string(use StringRegExp) with another string, search the file fit StringRegExp
; 
; Parameter(s):    $sPath = file path
;    $rPath = StringRegExp pattern of file path
;    $sPathExclude = Excelude path, dispart by comma(,)
;    $rPattern = The string we want to replace in the file,use StringRegExp
;    $rReplace = The string we want as a replacement for $rPattern,if 0,return found files,else return replaced files 
;    $iCount =The number of times to execute the replacement in the string. The default is 0. Use 0 for global replacement.
; Requirement(s):  autoit v3.2.2.0
; Return Value(s): 
; On Success   Returns an array, the first element ($array[0]) contains the number of strings returned, the remaining elements ($array[1], $array[2], etc.) contain the found files or replaced files.

;
; On Failure 
;     @error=1 Path not found or invalid
;     @error=4 No File(s) Found
; Author(s):     [url="http://www.yidabu.com"]http://www.yidabu.com[/url]  一大步成功社区  [url="http://bbs.yidabu.com/forum-2-1.html"]http://bbs.yidabu.com/forum-2-1.html[/url]
; update:        20070125
;===============================================================================
;
;Chinese Version:
;Example:
;myReplaceStringInFile("D:\AutoIt\复件 article", "discuz", "BackUp,_private,images", "(?m)^tag:", "thisistag:")
; ===============================================================================
;
; Function Name:   myReplaceStringInFile
; Description::    用正则表达式批量搜索或替换文件中的字符串
; Parameter(s):    $sPath = 路径
;    $rPath = 匹配路径的正则表达式
;    $sPathExclude = 要排除的路径用,分隔多个
;    $rPattern = 要搜索的字符串的正则模式
;    $rReplace = 替换字符串支持反向引用省略表示只搜索不替换
;    $iCount 替换次数默认0进行全局替换
; Requirement(s):  autoit v3.2.2.0My.au3 [url="http://bbs.yidabu.com/thread-109-1.html"]http://bbs.yidabu.com/thread-109-1.html[/url]
; Return Value(s): 成功则返回数组$a[0]是结果数$a[1]是第一个结果
; Author(s):     [url="http://www.yidabu.com"]http://www.yidabu.com[/url]  一大步成功社区  [url="http://bbs.yidabu.com/forum-2-1.html"]http://bbs.yidabu.com/forum-2-1.html[/url]
; update:        20070125
;===============================================================================

Func myReplaceStringInFile($sPath, $rPath = 0, $sPathExclude = 0, $rPattern = 0, $rReplace = 0, $iCount = 0)
 ;yidabu.com提示$sRe 返回值,供递归调用非可选参数要在前面
 Local $sRe
 $sRe=myReplaceStringInFileTemp($sRe, $sPath, $rPath, $sPathExclude, $rPattern, $rReplace, $iCount)
 $sRe=StringStripWS($sRe,2)
 ;yidabu.com提示stringsplit时要注意参数1不能省否则会@cr和@lf两个分隔参数
 $sRe=StringSplit($sRe,@CRLF,1)
 Return $sRe
EndFunc   ;==>myReplaceStringInFile
Func myReplaceStringInFileTemp(ByRef $sRe, $sPath, $rPath = 0, $sPathExclude = 0, $rPattern = 0, $rReplace = 0, $iCount = 0)
 Local $hSearch, $sFile
 If Not FileExists($sPath) Then Return SetError(1, 1, "")
 $hSearch = FileFindFirstFile($sPath & "\*")
 If $hSearch = -1 Then Return SetError(4, 4, "")
 While 1
  $sFile = FileFindNextFile($hSearch)
  If @error Then
   SetError(0)
   ExitLoop
  EndIf
  
  ;yidabu.com提示已经被排除的路径就不要搜索子目录了
  If $sPathExclude And StringLen($sPathExclude) > 0 Then $sPathExclude = StringSplit($sPathExclude, ",")
  $bExclude = False
  If IsArray($sPathExclude) Then
   For $ii = 1 To $sPathExclude[0] Step 1
    If StringInStr($sPath & "\" & $sFile, $sPathExclude[$ii]) Then
     $bExclude = True
     ExitLoop
    EndIf
   Next
  EndIf
  If $bExclude Then ContinueLoop
  If StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") Then ;如果遇到目录就递归
   myReplaceStringInFileTemp($sRe, $sPath & "\" & $sFile, $rPath, $sPathExclude, $rPattern, $rReplace, $iCount)
   ContinueLoop ;跳过目录
  EndIf
  
  ;yidabu.com提示要求正则匹配路径且匹配失败时就跳过。
  If $rPath And (Not StringRegExp($sPath & "\" & $sFile, $rPath, 0)) Then ContinueLoop
  
  Local $s = FileRead($sPath & "\" & $sFile)
  ;不替换也就是查找
  If Not $rReplace Then
   If Not StringRegExp($s, $rPattern, 0) Then ContinueLoop
  ;执行替换
  Else
   ;将要替换的文件保存到BackUp目录以备意外情况下恢复
   Local $CopyDest = StringRegExpReplace($sPath & "\" & $sFile, "(.+?\\)([^\\]+$)", "$1BackUp\\$2", 1)
   If FileCopy($sPath & "\" & $sFile, $CopyDest, 9) Then ConsoleWrite("backup:" & $sPath & "\" & $sFile & @LF)
   $s = StringRegExpReplace($s, $rPattern, $rReplace, $iCount)
   If @error Then ContinueLoop;替换失败就不要打开文件了
   Local $hFile = FileOpen($sPath & "\" & $sFile, 2);清除原来内容
   FileWrite($hFile, $s)
   FileClose($hFile)
  EndIf
  $sRe &= $sPath & "\" & $sFile & @CRLF
 WEnd
 
 FileClose($hSearch)
 Return $sRe
EndFunc   ;==>myReplaceStringInFileTemp

;myReplaceStringInFile.  oÝ÷ Ù«­¢+ØíµåÉÉåAÉ¥¹Ðè(ìôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôô(ì(ìչѥ½¸9µèµåÉÉåAÉ¥¹Ð ÀÌØí¤(ìÍÉ¥ÁÑ¥½¸èèÈÔÈÈØíÉÉäÌÀÌÐÀìÈÀàØäìÈÌÐàÄìÈÔÄÜÄìÈÄÌØÀìÈÈÌÄÈíÍ¥ÑÌØÜÔÔìÈÀäàØìÈÄÌÀØì(ìAɵÑȡ̤èÀÌØí(ìIÅեɵ¹Ð¡Ì¤è(ìIÑÕɸY±Õ¡Ì¤èÍÕÍÍÉÑÕɸÑÉÕ±¥±ÉÑÕɸ±Í(ìÕÑ¡½È¡Ì¤èÄääØàìÈÈàÈÌìÈÜÐäÌìÈÔÄÀÐìÈÄÄÔÄìÌÄÀÌàìÈÄÌÀØìmÕÉ°ôÅÕ½Ðí¡ÑÑÀ輽̹å¥Ô¹½´½½ÉÕ´´È´Ä¹¡Ñµ°ÅÕ½Ðíu¡ÑÑÀ輽̹å¥Ô¹½´½½ÉÕ´´È´Ä¹¡Ñµ±l½Õɱt(ì(ìôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôô(ì()Õ¹µåÉÉåAÉ¥¹Ð ÀÌØí¤(%9½Ð%ÍÉÉä ÀÌØí¤Q¡¸(
½¹Í½±]É¥Ñ ÅÕ½ÐíÉɽÈé¹½ÐÉÉäÅÕ½ÐìµÀì
I1¤(íå¥Ô¹½´ÈÔÔÔÈìÌÄÀÌÐìØÔÌÀØìÌàÜÔÀíÉÉÉäÈÔÔÔÈìÌÄÀÌÐìÈÐÄàÈìÌØàÈÀìÈÈÈÌàí±Í(IÑÕɸ±Í(¹%(1½°ÀÌØíÌ(1½°ÀÌØíÍQµÀ(½ÈÀÌØí¤ôÀQ¼U   ½Õ¹ ÀÌØí¤´ÄMÑÀÄ(ÀÌØíÌôÀÌØí̵ÀìÀÌØí¤µÀìÌäìôÅÕ½ÐìÌäìµÀìÀÌØílÀÌØí¥tµÀìÌäìÅÕ½ÐìÌäìµÀì1(9áÐ(
½¹Í½±]É¥Ñ ÅÕ½ÐíÉÉ䥸èÅÕ½ÐìµÀì
I1µÀìÀÌØí̵ÀìÅÕ½ÐíÉÉ乸ÅÕ½ÐìµÀì
I1¤(IÑÕɸQÉÕ)¹Õ¹(íµåÉÉåAÉ¥¹Ð¸oÝ÷ ØLZ^ÖÚwa£   'âën­ªê-תê-jëh×6;Autoit Setup directory
Local $sPath="D:\AutoIt"
;search .au3 file only
Local $rPath="\.au3"
;don't search BackUp file or directory
Local $sPathExclude="BackUp,_private"
;the word to search
Local $rPattern="jdeb"
; only search,don't replace
$rReplace=0
Local $a=myReplaceStringInFile($sPath,$rPath,$sPathExclude,$rPattern,$rReplace)
myArrayPrint($a)oÝ÷ Ú·¬º[^jºÚÉ· }

result:

array begin:

0="38"

1="D:\AutoIt\Include\Array.au3"

2="D:\AutoIt\Include\Date.au3"

3="D:\AutoIt\Include\File.au3"

4="D:\AutoIt\Include\GuiEdit.au3"

5="D:\AutoIt\Include\GuiList.au3"

6="D:\AutoIt\Include\GuiListView.au3"

7="D:\AutoIt\Include\GuiStatusBar.au3"

8="D:\AutoIt\Include\String.au3"

9="D:\AutoIt\Include\A3LDateTimePick.au3"

10="D:\AutoIt\Include\A3LEventLog.au3"

11="D:\AutoIt\Include\A3LFile.au3"

12="D:\AutoIt\Include\A3LHeader.au3"

13="D:\AutoIt\Include\A3LImageList.au3"

14="D:\AutoIt\Include\A3LIPAddress.au3"

15="D:\AutoIt\Include\A3LLanMan.au3"

16="D:\AutoIt\Include\A3LLibrary.au3"

17="D:\AutoIt\Include\A3LListBox.au3"

18="D:\AutoIt\Include\A3LListView.au3"

19="D:\AutoIt\Include\A3LMemory.au3"

20="D:\AutoIt\Include\A3LMenu.au3"

21="D:\AutoIt\Include\A3LMonthCal.au3"

22="D:\AutoIt\Include\A3LNetShare.au3"

23="D:\AutoIt\Include\A3LProgress.au3"

24="D:\AutoIt\Include\A3LScreenCap.au3"

25="D:\AutoIt\Include\A3LSecurity.au3"

26="D:\AutoIt\Include\A3LStatus.au3"

27="D:\AutoIt\Include\A3LStructs.au3"

28="D:\AutoIt\Include\A3LTabControl.au3"

29="D:\AutoIt\Include\A3LToolbar.au3"

30="D:\AutoIt\Include\A3LToolTip.au3"

31="D:\AutoIt\Include\A3LTreeView.au3"

32="D:\AutoIt\Include\A3LWinAPI.au3"

33="D:\AutoIt\SciTe\AutoIt3Wrapper\AutoIt3Wrapper.au3"

34="D:\AutoIt\SciTe\defs\UpdateDefs.au3"

35="D:\AutoIt\SciTe\defs\UpdateDefs\UpdateDefs.au3"

36="D:\AutoIt\SciTe\SciteConfig.au3"

37="D:\AutoIt\SciTe\Tidy\tidy.au3"

38="D:\AutoIt\code\test2.au3"

array end.

Chinese version here:

http://bbs.yidabu.com/thread-112-1.html

Edited by yidabu

my UDF:myReplaceStringInFile suport StringRegExp and subdirectorymyFileListToArray suport StringRegExp and subdirectorymyImageIdentify get all information from image, use Image Magick com supportautoit in Chinaautoit 论坛yidabu成功社区

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