#include <Array.au3>
#include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $WorkDir;需要搜索的目录
Global $BakDir = 'E:\htm测试\html备份\'
Global $LogFile = @ScriptDir & '\替换记录.txt'
Global $File_Path, $File_Array
$Form1_1 = GUICreate("Form1", 633, 105, 2, 2, BitOR($WS_POPUP, $WS_BORDER), _
BitOR($WS_EX_DLGMODALFRAME, $WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
$Button1 = GUICtrlCreateButton("文 本 替 换", 520, 80, 105, 20)
$Button2 = GUICtrlCreateButton("编 码 替 换", 350, 80, 105, 20)
$Button3 = GUICtrlCreateButton("退 出", 150, 80, 105, 20)
$Label1 = GUICtrlCreateLabel("搜索字符", 5, 30, 52, 17)
$Label2 = GUICtrlCreateLabel("替换字符", 5, 57, 52, 17)
$Label3 = GUICtrlCreateLabel("文件地址", 5, 7, 52, 17)
$Label4 = GUICtrlCreateLabel("当前完成", 5, 80, 65, 17)
$Input1 = GUICtrlCreateInput("", 62, 28, 560, 20)
$Input2 = GUICtrlCreateInput("", 62, 56, 560, 20)
$Input3 = GUICtrlCreateInput("粘贴路径", 62, 5, 560, 17)
GUICtrlSetState ( -1, $GUI_FOCUS )
$Input4 = GUICtrlCreateInput("", 62, 82, 50, 17)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1;文本替换
$WorkDir = GUICtrlRead($Input3)
_Replacement()
Case $Button2;编码替换
$WorkDir = GUICtrlRead($Input3)
If $WorkDir = "" Then
MsgBox(0, 0, "无路径!", 1)
EndIf
_format()
Case $Button3;退出
Exit
EndSwitch
WEnd
Func _format()
If $WorkDir = "" Then
MsgBox(0, 0, "无路径!", 1)
Return
EndIf
Dim $szDrive, $szDir, $szFName, $szExt
$File_Path2 = _PathSplit($WorkDir, $szDrive, $szDir, $szFName, $szExt)
$File = $File_Path2[1] & $File_Path2[2] & 'AutoIt3 TOC.hhc'
$str = String(FileRead($File))
$str = StringRegExpReplace($str, '(?<=\w)%20(?=\w)', ' ')
If Not @error And @extended > 0 Then
FileMove($File, $File & '.bak')
FileWrite($File, $str)
EndIf
Local $ts = TimerInit() ;返回一个时间戳(开始计算耗时)
Local $File_Array = _FileSearchToArray($WorkDir, '*.htm') ;遍历搜索*.htm文件(包括子目录)到数组 $File_Array[$i]=完整路径+文件名 $i 从 1 开始循环
If @error Then
MsgBox(4096, '错误', '未搜索到文件 ')
Return
EndIf
Local $FR, $thStr, $FLog, $x = 0, $ISO, $UTF
For $i = 1 To $File_Array[0]
$FR = String(FileRead($File_Array[$i]))
$ISO = StringInStr($FR, 'charset=iso-8859-1"')
$UTF = StringInStr($FR, 'charset=UTF-8"')
If $ISO <> 0 Or $UTF <> 0 Then ;该文件含有iso-8859-1字符
FileMove($File_Array[$i], $BakDir & StringRegExpReplace($File_Array[$i], '.+?\\html\\', ''), 9) ;备份文件及目录结构
If $ISO <> 0 Then
$string = '(?<=charset=)iso-8859-1(?=")'
ElseIf $UTF <> 0 Then
$string = '(?<=charset=)UTF-8(?=")'
EndIf
$thStr = StringRegExpReplace($FR, $string, 'GB2312')
FileWrite($File_Array[$i], $thStr) ;创建ANSI格式文件
$FLog &= $File_Array[$i] & @CRLF ;做输出记录
$x += 1
GUICtrlSetData($Input4, $x);实时显示修改数
EndIf
Next
Local $te = Round(TimerDiff($ts))
FileDelete($LogFile)
FileWrite($LogFile, '共修改 ' & $x & ' 个文件,耗时 ' & $te & ' 毫秒。以下为已替换并改为ANSI格式的文件:' & @CRLF & $FLog)
ShellExecute($LogFile)
GUICtrlSetState ( $Button2, $GUI_DISABLE );禁用 编码转换 按钮
EndFunc ;==>_format
Func _FileSearchToArray($sPath, $sFilter = '*.*') ;遍历搜索*.htm文件(包括子目录)到数组
If StringRight($sPath, 1) <> '\' Then $sPath &= '\' ;路径变量加 "" 结尾
If Not StringInStr(FileGetAttrib($sPath), 'D') Then Return SetError(1, 0, 0) ;无此目录,则返回错误
If StringReplace($sFilter, ' ', '') = '' Then $sFilter = '*';文件类型变量为空,则变量='*'
Local $sOUT = @ScriptDir & '\$$$_____temp_filelist.txt'
FileClose(FileOpen($sOUT, 2));关闭此前打开的文本文件
RunWait(@ComSpec & ' /c ' & 'dir ' & $sPath & $sFilter & ' /a:-d /b /s > ' & $sOUT, '', 0);写记录文件
Local $FR = FileRead($sOUT);读记录文件
FileDelete($sOUT);删除记录文件
Local $afilelist = StringRegExp('1' & @CRLF & $FR, '\V+', 3);返回全局匹配的数组
If @error Or UBound($afilelist) = 1 Then Return SetError(2, 0, 0) ;该目录无文件
$afilelist[0] = UBound($afilelist) - 1
Return $afilelist
EndFunc ;==>_FileSearchToArray
Func _Replacement()
If $WorkDir = "" Then
MsgBox(0, 0, "无路径!", 1)
Return
EndIf
GUICtrlSetData($Input4, "");清空修改数
$Count = 0
$Original = GUICtrlRead($Input1)
$replace = GUICtrlRead($Input2)
;遍历搜索*.htm文件(包括子目录)到数组 $File_Array[$i]=完整路径+文件名 $i 从 1 开始循环:
Local $File_Array = _FileSearchToArray($WorkDir, '*.htm')
For $i = 1 To $File_Array[0]
$retval = _ReplaceStringInFile($File_Array[$i], $Original, $replace, 0, 1)
If $retval <> "" Then
$Count = $Count + $retval
GUICtrlSetData($Input4, $Count);实时显示修改数
EndIf
If $i = $File_Array[0] Then
MsgBox(0, 0, "完成", 1)
GUICtrlSetData($Input1, "")
GUICtrlSetData($Input2, "")
Else
EndIf
Next
EndFunc ;==>_Replacement
翻到第三页也没找到这个帖子,回头看原来置顶了。。。。