#include <_Ini.au3>
; 函数名:_IniRenameKey;========================================================================
; 说明: 重命名INI结构文件里面的关键字。
; 语法: _IniRenameKey('ini文件路径', '字段名', '原关键字', '新关键字'[, '打开模式'])
; 返回值:成功: 返回1。
; 失败: 返回0。并设置 @Error 为以下值:
; @Error=1 原关键字不存在;
; @Error=2 新关键字已存在;
; @Error=3 新关键字为空或仅含有空格或首个非空格字符为“[”
; @Error=4 文件具有只读属性或者无法写入数据。
;==========================================================================================start
Func _IniRenameKey($iFile, $Section, $Key, $new_key, $mode = 0)
If StringRegExp($new_key, '^\s*(?:\[.*)?$') Then Return SetError(3, 0, 0)
_IniRead($iFile, $Section, $Key, @LF, $mode)
If @error Then Return SetError(1, 0, 0)
_IniRead($iFile, $Section, $new_key, @LF, $mode)
If Not @error Then Return SetError(2, 0, 0)
Local $s_File = _FileRead($iFile, $mode)
Local $_Section = StringRegExpReplace($Section, '^\h*(.+?)\h*$', '$1')
$_Section = StringRegExpReplace($_Section, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)', '\\$1')
$_Section = '\[\h*' & $_Section & '\h*\]'
Local $_key = StringRegExpReplace($Key, '^\h*(.+?)\h*$', '$1')
$_key = StringRegExpReplace($_key, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\|\=)', '\\$1')
Local $_new_key = StringRegExpReplace($new_key, '^\h*(.+?)\h*$', '$1')
$_new_key = StringRegExpReplace($_new_key, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\|\=)', '\\$1')
Local $resdata = StringRegExpReplace(@CRLF & $s_File, '(?s)(?i)(.*\r?\n\h*' & $_Section & '.*?\r?\n)\h*(' & $_key & ')\h*(\=.*)', '${1}' & $_new_key & '$3')
$resdata = StringTrimLeft($resdata, 2)
Local $FO = FileOpen($iFile, $mode + 2)
Local $Write = FileWrite($FO, $resdata)
FileClose($FO)
If $Write = 0 Then Return SetError(4, 0, 0)
Return 1
EndFunc ;==>_IniRenameKey