#AutoIt3Wrapper_Icon=E:\Icon_1.ico
#include <Array.au3>
#include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region 英-汉数据数组
Global $Chinese[11][2]
$Chinese[0][0]="Below is a complete list of the functions available in AutoIt"
$Chinese[0][1]="下面是 AutoIt 函数的完整列表"
$Chinese[1][0]="Calculates the absolute value of a number"
$Chinese[1][1]="计算数的绝对值"
$Chinese[2][0]="Calculates the arcCosine of a number"
$Chinese[2][1]="计算数的反余弦值"
$Chinese[3][0]="Registers an Adlib function"
$Chinese[3][1]="注册 Adlib 函数"
$Chinese[4][0]="Unregisters an adlib function"
$Chinese[4][1]="注销 Adlib 函数"
$Chinese[5][0]="Returns the ASCII code of a character"
$Chinese[5][1]="返回字符的 ASCII 码"
$Chinese[6][0]="Returns the unicode code of a character"
$Chinese[6][1]="返回字符的 Unicode 代码"
$Chinese[7][0]="Calculates the arcsine of a number"
$Chinese[7][1]="计算数的反正弦值"
$Chinese[8][0]="Assigns a variable by name with the data"
$Chinese[8][1]="给变量赋值"
$Chinese[9][0]="Calculates the arctangent of a number"
$Chinese[9][1]="计算数的反正切值"
$Chinese[10][0]="Changes the operation of various AutoIt functions/parameters"
$Chinese[10][1]="调整 Autoit 函数或参数的操作方式"
#EndRegion 英-汉数据数组
;_ArrayDisplay($Chinese,'Debug~~~')
# ==============================================================================
Global $BakDir = 'E:\htm备份\html\'
Global $LogFile = @ScriptDir & '\替换记录.txt'
Global $filesl[1] ;拖入的文件数组
Global $File_Path, $File_Array
Global $A = "Calculates the absolute value of a number"
Global $B = "计算数的绝对值"
If FileExists(@ScriptDir & '\替换.ini') = 0 Then
_FileCreate(@ScriptDir & '\替换.ini')
IniWrite(@ScriptDir & '\替换.ini', "配置", "路径", "")
;IniWrite(@ScriptDir & '\替换.ini', "配置", "换码", "0")
Else
$Path = IniRead(@ScriptDir & '\替换.ini', "配置", "路径", "0")
;$State = IniRead(@ScriptDir & '\替换.ini', "配置", "换码", "0")
EndIf
$Form1_1 = GUICreate("Form1", 633, 105, 2, 2, BitOR($WS_POPUP, $WS_BORDER), _
BitOR($WS_EX_DLGMODALFRAME, $WS_EX_TOPMOST, $WS_EX_TOOLWINDOW, $WS_EX_ACCEPTFILES)) ;$WS_EX_ACCEPTFILES支持拖放
$Button1 = GUICtrlCreateButton("文 本 替 换", 520, 80, 105, 20)
$Button2 = GUICtrlCreateButton("编 码 替 换", 345, 80, 105, 20)
$Button3 = GUICtrlCreateButton("退 出", 170, 80, 105, 20)
$Label1 = GUICtrlCreateLabel("搜索字符", 5, 30, 52, 17)
$Label2 = GUICtrlCreateLabel("替换字符", 5, 57, 52, 17)
$Label3 = GUICtrlCreateLabel("文件地址", 5, 7, 52, 17)
$Label4 = GUICtrlCreateLabel("当前完成", 5, 85, 65, 17)
$Input3 = GUICtrlCreateInput($Path, 62, 5, 560, 17);输入文件路径
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
$Input1 = GUICtrlCreateInput("", 62, 28, 560, 20)
$Input2 = GUICtrlCreateInput("", 62, 56, 560, 20)
GUICtrlSetState(-1, $GUI_FOCUS)
$Input4 = GUICtrlCreateInput("", 62, 82, 80, 17)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $GUI_EVENT_DROPPED ;拖入文件 MsgBox(0,0,$GUI_EVENT_DROPPED)=-13
Local $mouse = GUIGetCursorInfo($Form1_1);获取相对于 GUI 窗口的鼠标光标位置,返回包含光标信息的 5 元素数组
;发送命令到指定控件,$mouse[4]=光标下控件ID,'GetSelected'=返回编辑框选定的文本
Local $_str = ControlCommand($Form1_1, '', $mouse[4], 'GetSelected', '');返回拖放文件的完整路径+文件名
GUICtrlSetData($Input3, $_str);完整路径+文件名写入地址框
Case $Button1 ;文本替换
_Abcdef();_Replacement(GUICtrlRead($Input3));读取路径作为传递的参数
Case $Button2 ;编码替换
_format(GUICtrlRead($Input3));读取路径作为传递的参数
Case $Button3 ;退出
Global $Path01 = GUICtrlRead($Input3)
;Global $State0 = GUICtrlGetState($Button2)
;If $State0 = 144 Then $State = 1;禁用=144 ,启用= 80 设置 0=启用 1=禁用
If $Path01 = '' Then $State = 0
IniWrite(@ScriptDir & '\替换.ini', "配置", "路径", $Path01)
;IniWrite(@ScriptDir & '\替换.ini', "配置", "换码", $State)
Exit
EndSwitch
WEnd
Func _format($WorkDir) ;编码替换
If $WorkDir = "" Or $WorkDir = "输入文件路径,或直接拖入文件" Then Return MsgBox(0, 0, "无路径!", 1)
Local $szDrive, $szDir, $szFName, $szExt
#Region 处理 目录文件 开始
Local $File_Path2 = _PathSplit($WorkDir, $szDrive, $szDir, $szFName, $szExt);完整路径拆分
Local $File = $File_Path2[1] & $File_Path2[2] & 'AutoIt3 TOC.hhc'
Local $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
#EndRegion 处理 目录文件 开始
Local $ts = TimerInit() ;返回一个时间戳(开始计算耗时)
Local $File_Array = _FileSearchToArray($WorkDir, '*.htm') ;遍历搜索*.htm文件(包括子目录)到数组 $File_Array[$i]=完整路径+文件名 $i 从 1 开始循环
If @error Then Return MsgBox(4096, '错误', '未搜索到文件 ')
Local $FR, $thStr, $FLog1 = '', $FLog2 = '', $x1 = 0, $x2 = 0
For $i = 1 To $File_Array[0]
$FR = String(FileRead($File_Array[$i]))
Local $ChangeBj1 = 0, $ChangeBj2 = 0 ;修改标记
$FR = StringRegExpReplace($FR, '(?i)(?<=h1>)Function Reference(?=</h1>)', '函 数 参 考')
If Not @error And @extended > 0 Then $ChangeBj1 = 1
$FR = StringRegExpReplace($FR, '(?i)(?<=b>)Parameters(?=</b>)', '参 数')
If Not @error And @extended > 0 Then $ChangeBj1 = 1
$FR = StringRegExpReplace($FR, '(?i)(?<=b>)Return Value(?=</b>)', '返 回 值')
If Not @error And @extended > 0 Then $ChangeBj1 = 1
$FR = StringRegExpReplace($FR, '(?i)(?<=b>)Remarks(?=</b>)', '备 注')
If Not @error And @extended > 0 Then $ChangeBj1 = 1
$FR = StringRegExpReplace($FR, '(?i)(?<=b>)Related(?=</b>)', '相 关 函 数')
If Not @error And @extended > 0 Then $ChangeBj1 = 1
$FR = StringRegExpReplace($FR, '(?i)(?<=b>)Example(?=</b>)', '函 数 示 例')
If Not @error And @extended > 0 Then $ChangeBj1 = 1
$FR = StringRegExpReplace($FR, '(?i)(?<=valign="top">)Success(?=:</td>)', '成 功')
If Not @error And @extended > 0 Then $ChangeBj1 = 1
$FR = StringRegExpReplace($FR, '(?i)(?<=valign="top">)Failure(?=:</td>)', '失 败')
If Not @error And @extended > 0 Then $ChangeBj1 = 1
$FR = StringRegExpReplace($FR, '(?i)(?<=h1>)Keyword Reference(?=</h1>)', '关 键 字 参 考')
If Not @error And @extended > 0 Then $ChangeBj1 = 1
$FR = StringRegExpReplace($FR, '(?i)(?<=b>)See Also(?=</b>)', '参 考 资 料')
If Not @error And @extended > 0 Then $ChangeBj1 = 1
$FR = StringRegExpReplace($FR, '(?i)(?<=b>)\[optional\](?=</b>)', '可选参数');b>[optional]</b>
If Not @error And @extended > 0 Then $ChangeBj1 = 1 ;b>[optional]</b>
$FR = StringRegExpReplace($FR, '(?i)(?<=">)Old(?=</th>)', '旧 名')
If Not @error And @extended > 0 Then $ChangeBj1 = 1 ; >Old</th>
$FR = StringRegExpReplace($FR, '(?i)(?<=">)New(?=</th>)', '新 名')
If Not @error And @extended > 0 Then $ChangeBj1 = 1 ;>New</th>
$FR = StringRegExpReplace($FR, '(?i)(?<=">)Comments(?=</th>)', '注 解')
If Not @error And @extended > 0 Then $ChangeBj1 = 1 ;>Comments</th>
$FR = StringRegExpReplace($FR, '(?<=\s)Success:', '成 功:')
If Not @error And @extended > 0 Then $ChangeBj1 = 1 ;>Comments</th>
$FR = StringRegExpReplace($FR, '(?<=br>\h)Failure(?=:)', '失 败:');??????????????
If Not @error And @extended > 0 Then $ChangeBj1 = 1 ;br>Failure:
$FR = StringRegExpReplace($FR, '(?i)(?<=>)Returns 0.(?=</)', '返回 0.')
If Not @error And @extended > 0 Then $ChangeBj1 = 1;>Returns 0.</
$FR = StringRegExpReplace($FR, '(?<=b>)User Defined Function(?=</b>)', '用 户 定 义 函 数')
If Not @error And @extended > 0 Then $ChangeBj1 = 1 ;b>User Defined Function</b>
$FR = StringRegExpReplace($FR, '(?<=b>)Description(?=</b>)', '描 述')
If Not @error And @extended > 0 Then $ChangeBj1 = 1 ;b>Description</b>
$FR = StringRegExpReplace($FR, '(?<=\s)user defined functions Reference(?=</h1>)', '用户定义函数参考')
If Not @error And @extended > 0 Then $ChangeBj1 = 1 ;user defined functions Reference</h1>
$FR = StringRegExpReplace($FR, '(?<=p>)user defined functions Reference(?=\s)', '要使用它们,你需要添加语句:');????????????
If Not @error And @extended > 0 Then $ChangeBj1 = 1 ;<p>When using them you need to add a
$FR = StringRegExpReplace($FR, '(?<=p>)user defined functions Reference(?=. )', '下面是定义在 AutoIt 中的用户定义函数完整列表');???????????
If Not @error And @extended > 0 Then $ChangeBj1 = 1 ;<p>Below is a complete list of the user defined functions available in AutoIt
$FR = StringRegExpReplace($FR, '(?<=\s)Click on a user defined function name for a detailed description.(?=</p>)', '点击用户定义函数的名称查阅详细描述.')
If Not @error And @extended > 0 Then $ChangeBj1 = 1 ;Click on a user defined function name for a detailed description.</p>
If $ChangeBj1 Then ;有汉化字符
$FLog1 &= $File_Array[$i] & @CRLF ;做输出记录
$x1 += 1
EndIf
;--------以上先替换这些基本汉化内容
$thStr = StringRegExpReplace($FR, '(?<=charset=)iso-8859-1(?=")|(?<=charset=)Windows-1252(?=")', 'GB2312', 1)
If Not @error And @extended > 0 Then ;有修改编码字符
$ChangeBj2 = 1
$FLog2 &= $File_Array[$i] & @CRLF ;做输出记录
$x2 += 1
EndIf
;--------以上替换编码格式字符内容
If $ChangeBj1 Or $ChangeBj2 Then ;有修改记录
FileMove($File_Array[$i], $BakDir & StringRegExpReplace($File_Array[$i], '.+?\\html\\', ''), 9) ;备份文件及目录结构
FileWrite($File_Array[$i], $thStr) ;创建ANSI格式文件
GUICtrlSetData($Input4, $x1 & ' - ' & $x2);实时显示修改数
EndIf
Next
Local $te = Round(TimerDiff($ts))
FileDelete($LogFile)
Local $textOut = '耗时 ' & $te & ' 毫秒' & @CRLF & @CRLF
If $FLog1 <> '' Then $textOut &= '汉化基本字符文件 ' & $x1 & ' 个,以下为记录:' & @CRLF & $FLog1 & @CRLF & @CRLF
If $FLog2 <> '' Then $textOut &= '修改为GB2312 编码 ANSI格式的文件 ' & $x2 & ' 个,以下为记录:' & @CRLF & $FLog2 & @CRLF
FileWrite($LogFile, $textOut)
ShellExecute($LogFile)
;GUICtrlSetState($Button2, $GUI_DISABLE);禁用 编码转换 按钮
EndFunc ;==>_format
Func _Abcdef()
For $k = 0 To UBound($Chinese)
If $Chinese[$k][0] = "" Then Return MsgBox(0, 0, "替换完毕", 1)
GUICtrlSetData($Input1,$Chinese[$k][0]);读入搜索字串到 $Input1
Sleep(550)
GUICtrlSetData($Input2,$Chinese[$k][1]);读入替换字串到 $Input2
Sleep(550)
_Replacement(GUICtrlRead($Input3));读取路径作为传递的参数
Next
EndFunc
Func _Replacement($WorkDir) ;文本替换
If $WorkDir = "" Or $WorkDir = "输入文件路径,或直接拖入文件" Then Return MsgBox(0, 0, "无路径!", 1)
GUICtrlSetData($Input4, "");清空修改数
Local $Count = 0
Local $Original = GUICtrlRead($Input1)
Local $replace = GUICtrlRead($Input2)
;遍历搜索*.htm文件(包括子目录)到数组 $File_Array[$i]=完整路径+文件名 $i 从 1 开始循环:
Local $File_Array = _FileSearchToArray($WorkDir, '*.htm')
For $i = 1 To UBound($File_Array);[0]+1
$retval = _ReplaceStringInFile($File_Array[$i], $Original, $replace, 0, 1);替换文本文件的字符串
If Not @error Then
$Count += $retval
GUICtrlSetData($Input4, $Count);实时显示修改数
EndIf
Next
MsgBox(0, 0, "完成。已替换 " & $Count & " 处。", 1)
GUICtrlSetData($Input1, "")
GUICtrlSetData($Input2, "")
EndFunc ;==>_Replacement
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));清空记录文本
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sPath & $sFilter = ' & $sPath & $sFilter & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
RunWait(@ComSpec & ' /c ' & 'dir "' & $sPath & $sFilter & '" /a:-d /b /s > ' & $sOUT, '', 0);写记录文件
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sPath & $sFilter = ' & $sPath & $sFilter & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
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