找回密码
 加入
搜索
查看: 1109|回复: 4

[AU3基础] [已解决]如何找出txt所有夸弧內的關鍵字並帶出下面幾行的文字

[复制链接]
发表于 2018-2-1 16:27:19 | 显示全部楼层 |阅读模式
本帖最后由 ipmitool 于 2018-2-2 13:36 编辑

testconfig.txt
[0001]
A1
A2
A3

[0002]
B1
B2
B3

[0003]
C1
C2
C3

[0004]
D1
D2
D3

[0005]
E1
E2
E3
;;;;;;;;; Read testconfig.txt ;;;;;;;;;

$testerread = _IniReadSection_line(@ScriptDir & "\testconfig.txt", '0001')

$tester0=$testerread[0]
$tester1=$testerread[1]
$tester2=$testerread[2]

MsgBox(0,"111",$tester0)
MsgBox(0,"111",$tester1)
MsgBox(0,"111",$tester2)

;;;;;;;;; Read testconfig.txt ;;;;;;;;;






#include-Once

#cs\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #include <_Ini.au3> \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

        ﹛﹛晤迡掛滲杅踱腔埻秪ㄩ
        ﹛﹛1ㄛ囀离滲杅勤衾議虳ini恅璃蚕衾晤鎢恀枙黍迡渣昫ㄛ掛踱炵蹈滲杅崝樓湖羲耀宒(32)ㄛ眕淏黍迡﹝
        ﹛﹛2ㄛ囀离滲杅衄虴杅擂躺衄32767趼睫褫眕掩淏都殿隙ㄛ掛踱炵蹈滲杅拸森癹秶﹝

        ﹛﹛絞唳掛ㄩv1.2.3.0

        ﹛﹛滲杅佽隴ㄩ
        ﹛﹛_IniDelete-----------;植INI賦凳恅璃笢刉壺議跺瑩硉.
        ﹛﹛_IniRead-------------;植INI賦凳恅璃笢黍議跺瑩硉.
        ﹛﹛_IniReadSection------;植INI賦凳恅璃笢黍議跺趼僇笢腔垀衄壽瑩趼摯硉.
        ﹛﹛_IniReadSectionNames-;植INI賦凳恅璃笢黍垀衄趼僇靡.
        ﹛﹛_IniRenameSection----;笭韜靡INI賦凳恅璃爵醱腔趼僇靡.
        ﹛﹛_IniRenameKey--------;笭韜靡INI賦凳恅璃爵醱腔壽瑩趼.
        ﹛﹛_IniWrite------------;迡珨跺瑩硉善INI賦凳恅璃.
        ﹛﹛_IniWriteSection-----;蔚杅擂迡善INI賦凳恅璃腔珨跺趼僇.

        ﹛﹛_IniReadSection_str--;植(準)梓袧INI賦凳恅璃笢黍議跺趼僇笢腔垀衄趼睫.
        ﹛﹛_IniReadSection_line-;植(準)梓袧INI賦凳恅璃笢黍議跺趼僇笢腔俴趼睫揹.
        ﹛﹛_FileRead------------;蚚硌隅腔FileOpen湖羲耀宒殿隙黍恅璃腔趼睫揹.
        ﹛﹛_IniDelete_line------;植(準)梓袧INI賦凳恅璃笢刉壺(漪衄)壽瑩趼腔俴.

        ﹛﹛蛁砩ㄩ
        ﹛﹛勤衾杻忷腔瑩迵硉ㄛ※abc=123=AAA§ 埻寀奻※abc§茼蜆符峈壽瑩趼ㄛ※123=AAA§峈硉ㄛ
        ﹛﹛掛砑寞毓眳ㄛ筍橇腕衄奀緊褫夔頗衄※abc=123§峈壽瑩趼ㄛ※AAA§峈硉腔剒ㄗ囀离滲杅岆羶腕恁腔ㄘㄛ
        ﹛﹛秪森ㄛ涴爵祥秶峈菴珨笱錶ㄛ硐剒蛁砩狟撓萸ㄗ絞ㄛ硐衄1跺脹瘍憩拸剒蛁砩涴虳賸ㄘㄩ
        ﹛﹛1ㄛ_IniReadSection 涴跺蔚悵厥峈躺巠蚚菴珨笱錶ㄛ※abc§峈壽瑩趼ㄛ※123=AAA§峈硉
        ﹛﹛2ㄛ_IniDelete﹜_IniRead﹜_IniRenameKey﹜_IniWrite 褫肮奀巠蚚謗笱錶﹝

        ﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛-- Afan -- http://www.autoitx.com -- 2010-6-28 --載陔衾 2015-06-07
#ce\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


; 滲杅靡:_IniDelete;=============================================================================
; 佽隴:﹛植INI賦凳恅璃笢刉壺議跺杅硉﹝
; 逄楊:﹛_IniDelete('ini恅璃繚噤', '趼僇靡'[, '壽瑩趼'[, 湖羲耀宒]])
; 統杅:﹛褫恁統杅'壽瑩趼'ㄛ猁刉壺腔壽瑩趼ㄛ祥硌隅壽瑩趼麼妏蚚Default壽瑩趼寀淕跺趼僇蔚掩刉壺﹝
; 殿隙硉:傖髡: 殿隙1﹝
;﹛﹛﹛﹛囮啖: 殿隙0﹝甜扢离 @Error 峈眕狟硉ㄩ
;﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛@Error=1 恅璃祥湔婓˙
;﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛@Error=2 趼僇祥湔婓˙
;﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛@Error=3 壽瑩趼祥湔婓
;﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛@Error=4 恅璃撿衄硐黍扽俶麼氪拸楊迡杅擂扢离﹝
;==========================================================================================start
Func _IniDelete($iFile, $Section, $key = '', $mode = 0)
        If Not FileExists($iFile) Then Return SetError(1, 0, 0)
        Local $s_File = _FileRead($iFile, $mode)
        Local $_Section = StringRegExpReplace($Section, '^\h*(.+?)\h*$', '$1')
        Local $_key = StringRegExpReplace($key, '^\h*(.+?)\h*$', '$1')
        $_Section = StringRegExpReplace($_Section, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)', '\\$1')
        $_Section = '\[\h*' & $_Section & '\h*\]'
        $_key = StringRegExpReplace($_key, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\|\=)', '\\$1')
        If Not StringRegExp(@CRLF & $s_File, '\r?\n\h*' & $_Section & '\s*') Then Return SetError(2, 0, 0)
        If $key <> '' And $key <> Default Then
                Local $vr = _IniRead($iFile, $Section, $key, @LF, $mode)
                If $vr = @LF Then Return SetError(3, 0, 0)
                Local $s_data = StringRegExpReplace(@CRLF & $s_File & @CRLF, '(?si)(.*?\r?\n\h*' & $_Section & '\h*\r?\n.*?)(\h*' & $_key & '\h*\=.*?\r?\n)(.*)', '$1$3')
        Else
                Local $s_data = StringRegExpReplace(@CRLF & $s_File & @CRLF & '[', '(?si)(.*?\r?\n)(\h*' & $_Section & '.*?)(\r?\n\h*\[.*)', '$1$3')
        EndIf
        $s_data = StringTrimLeft($s_data, 2)
        $s_data = StringTrimRight($s_data, 3)
        Local $FO = FileOpen($iFile, $mode + 2)
        Local $Write = FileWrite($FO, $s_data)
        FileClose($FO)
        If $Write = 0 Then Return SetError(4, 0, 0)
        Return 1
EndFunc   ;==>_IniDelete


; 滲杅靡:_IniRead;=============================================================================
; 佽隴:﹛植INI賦凳恅璃笢黍議跺杅硉﹝
; 逄楊:﹛_IniRead('ini恅璃繚噤', '趼僇靡', '壽瑩趼'[, '蘇硉'[, 湖羲耀宒]])
; 殿隙硉:傖髡: 殿隙硌隅腔壽瑩趼腔硉﹝
;﹛﹛﹛﹛囮啖: 殿隙蘇硉﹝INI恅璃麼趼僇祥湔婓麼黍渣昫 蔚扢离 @error=1˙@Error=2 壽瑩趼祥湔婓﹝
;==========================================================================================start
Func _IniRead($iFile, $Section, $key, $default = '', $mode = 0)
        Local $s_Sec = _IniReadSection_str($iFile, $Section, $mode)
        If @error = 1 Then Return SetError(1, 0, $default)
        If @error = 2 Then Return SetError(3, 0, $default)
        Local $_key = StringRegExpReplace($key, '^\h*|\h*$', '')
        $_key = StringRegExpReplace($_key, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\|\=)', '\\$1')
        Local $aVal = StringRegExp($s_Sec, '(?mi)^\h*' & $_key & '\h*\=\h*(\V*?)\s*$', 1)
        If @error Then Return SetError(2, 0, $default)
        Return $aVal[0]
EndFunc   ;==>_IniRead


; 滲杅靡:_IniReadSection;======================================================================
; 佽隴:﹛植INI賦凳恅璃笢黍議跺趼僇笢腔垀衄壽瑩趼麼硉﹝
;﹛﹛﹛﹛埻IniReadSection滲杅硐衄婓趼僇笢腔32767趼睫褫眕掩淏都殿隙,掛滲杅拸森癹秶(場笪婓衾森)﹝
;﹛﹛﹛﹛埻IniReadSection滲杅郣善議虳恅璃杻忷腔隙陬摯遙俴郪磁頗礿砦樟哿奧殿隙ㄛ掛滲杅頗俇淕黍堤﹝
;﹛﹛﹛﹛坳迵埻IniReadSection滲杅祥肮ㄩ祥殿隙拸壽瑩趼腔硉;暫拸壽瑩趼衱拸硉硐衄脹瘍腔珩祥掩殿隙﹝
; 逄楊:﹛_IniReadSection('ini恅璃繚噤', '趼僇靡'[, 湖羲耀宒])
; 殿隙硉:傖髡: 殿隙珨跺媼峎杅郪,笢ㄩ啋匼[0][0]=湮苤﹜啋匼[n][0]=壽瑩趼﹜啋匼[n][1]=勤茼腔杅硉﹝
;﹛﹛﹛﹛囮啖: 殿隙0﹝INI恅璃黍渣昫麼趼僇祥湔婓蔚扢离 @error=1˙趼僇囀峈諾蔚扢离 @error=2
;==========================================================================================start
Func _IniReadSection($iFile, $Section, $mode = 0)
        Local $s_Sec, $a_1, $ii = 1, $i
        $s_Sec = _IniReadSection_str($iFile, $Section, $mode)
        If @error Then Return SetError(@error, 0, 0)
        $a_1 = StringRegExp($s_Sec, '(?m)^\h*([^=;\s][^=\v]*?)\h*\=\h*(\V*?)\s*$', 3)
        If @error Then Return SetError(2, 0, 0)
        Local $2_array[UBound($a_1) / 2 + 1][2] = [[UBound($a_1) / 2]]
        For $i = 0 To UBound($a_1) - 1 Step 2
                $2_array[$ii][0] = $a_1[$i]
                $2_array[$ii][1] = $a_1[$i + 1]
                $ii += 1
        Next
        Return $2_array
EndFunc   ;==>_IniReadSection


; 滲杅靡:_IniReadSectionNames;===============================================================
; 佽隴:﹛植INI賦凳恅璃笢黍垀衄趼僇腔陓洘﹝
;﹛﹛﹛﹛埻IniReadSectionNames滲杅硐衄32767趼睫衄虴,掛滲杅拸森癹秶﹝
;﹛﹛﹛﹛坳迵埻IniReadSectionNames滲杅祥肮ㄩ
;﹛﹛﹛﹛    - 掛滲杅褫恁寁湖羲耀宒ㄛ眕晞俇淕黍堤垀衄趼僇靡﹝
;﹛﹛﹛﹛    - 祥殿隙俋醱拸"]"婦嬤腔趼僇靡;
;﹛﹛﹛﹛    - 祥殿隙諾腔趼僇靡ㄗ撈"[]"ㄘ;
;﹛﹛﹛﹛    - 褫殿隙蚕"[]"婦嬤腔俴囀垀衄趼睫峈趼僇靡ㄛ褫婦漪[]ㄛ"[a[b]]"殿隙靡峈"a[b]"腔趼僇靡ㄛ
;﹛﹛﹛﹛      剒黍"a[b]"眳濬婦漪"["麼"]"趼僇腔壽瑩趼麼硉ㄛ剒妏蚚 _IniReadSection() 滲杅﹝
; 逄楊:﹛_IniReadSectionNames('ini恅璃繚噤'[, 湖羲耀宒[, 梓祩]])
; 統杅:﹛褫恁統杅※梓祩§ㄛ梓祩=1奀殿隙腔杅郪0瘍啋匼峈杅郪腔啋匼杅講(蘇)˙梓祩=0奀壽敕0啋匼殿隙杅講
; 殿隙硉:傖髡: 殿隙珨跺漪衄垀衄趼僇靡腔杅郪﹝
;﹛﹛﹛﹛囮啖: 殿隙0﹝INI恅璃黍渣昫麼趼僇祥湔婓蔚扢离 @error=1
;=====================================================================================start
Func _IniReadSectionNames($iFile, $mode = 0, $flag = 1)
        Local $s_File = _FileRead($iFile, $mode)
        Local $Section = StringRegExp(@LF & $s_File, '\n\h*\[\h*(.*[^\s])\s*\]', 3)
        If @error Then Return SetError(1, 0, 0)
        If $flag = 0 Then Return $Section
        Local $_Section[UBound($Section) + 1], $i
        $_Section[0] = UBound($Section)
        For $i = 0 To $_Section[0] - 1
                $_Section[$i + 1] = $Section[$i]
        Next
        Return $_Section
EndFunc   ;==>_IniReadSectionNames


; 滲杅靡:_IniRenameSection;=====================================================================
; 佽隴:﹛笭韜靡INI賦凳恅璃爵醱腔趼僇﹝
; 逄楊:﹛_IniRenameSection('ini恅璃繚噤', '埻趼僇靡', '陔趼僇靡'[, 湖羲耀宒])
; 殿隙硉:傖髡: 殿隙1﹝
;﹛﹛﹛﹛囮啖: 殿隙0﹝甜扢离 @Error 峈眕狟硉ㄩ
;﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛@Error=1 埻趼僇祥湔婓˙
;﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛@Error=2 陔趼僇眒湔婓˙
;﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛@Error=3 恅璃撿衄硐黍扽俶麼氪拸楊迡杅擂﹝
;==========================================================================================start
Func _IniRenameSection($iFile, $Section, $new_Section, $mode = 0)
        Local $s_File = _FileRead($iFile, $mode)
        Local $_Section = StringRegExpReplace($Section, '^\h*(.+?)\h*$', '$1')
        $_Section = StringRegExpReplace($_Section, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)', '\\$1')
        $_Section = '\[\h*' & $_Section & '\h*\]'
        If Not StringRegExp(@CRLF & $s_File, '\r?\n\h*' & $_Section & '\s*') Then Return SetError(1, 0, 0)
        $new_Section = StringRegExpReplace($new_Section, '^\h*(.+?)\h*$', '$1')
        $new_Section = StringRegExpReplace($new_Section, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)', '\\$1')
        If StringRegExp(@CRLF & $s_File, '\r?\n\h*\[\h*' & $new_Section & '\h*\]\s*') Then Return SetError(2, 0, 0)
        Local $resdata = StringRegExpReplace(@CRLF & $s_File, '(?si)(.*\r?\n\h*)(' & $_Section & '\h*)(.*)', '${1}[' & $new_Section & ']$3', 1)
        $resdata = StringTrimLeft($resdata, 2)
        Local $FO = FileOpen($iFile, $mode + 2)
        Local $Write = FileWrite($FO, $resdata)
        FileClose($FO)
        If $Write = 0 Then Return SetError(3, 0, 0)
        Return 1
EndFunc   ;==>_IniRenameSection


; 滲杅靡:_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, '(?si)(.*\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


; 滲杅靡:_IniWrite;=============================================================================
; 佽隴:﹛迡珨跺硉善INI賦凳恅璃﹝
; 逄楊:﹛_IniWrite('ini恅璃繚噤', '趼僇靡', '壽瑩趼', '硉'[, 湖羲耀宒])
; 殿隙硉:傖髡: 淏都迡殿隙1˙眒湔婓眈肮腔壽瑩趼摯硉寀祥迡ㄛ殿隙2﹝
;﹛﹛﹛﹛囮啖: 殿隙0﹝恅璃撿衄硐黍扽俶麼氪拸楊迡杅擂﹝
;==========================================================================================start
Func _IniWrite($iFile, $Section, $key, $Val, $mode = 0)
        Local $_Val = StringRegExpReplace($Val, '^\h*(.+?)\h*$', '$1')
        Local $vr = _IniRead($iFile, $Section, $key, @LF, $mode)
        Local $Error = @error, $s_data, $Write
        If $vr = $_Val Then Return 2
        Local $s_File = _FileRead($iFile, $mode)
        Local $_Section = StringRegExpReplace($Section, '^\h*(.+?)\h*$', '$1')
        Local $_key = StringRegExpReplace($key, '^\h*(.+?)\h*$', '$1')
        Local $data = '[' & $_Section & ']' & @CRLF & $_key & '=' & $_Val
        Local $FO = FileOpen($iFile, $mode + 2)
        $_Section = StringRegExpReplace($_Section, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)', '\\$1')
        $_Section = '\[\h*' & $_Section & '\h*\]'
        $_key = StringRegExpReplace($_key, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\|\=)', '\\$1')
        $_Val = StringRegExpReplace($_Val, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\|\=)', '\\$1')
        Switch $Error
                Case 0
                        $s_data = StringRegExpReplace(@CRLF & $s_File & @CRLF & '[', '(?si)(.*?\r?\n\h*' & $_Section & '.*?\r?\n\h*' & $_key & '\h*\=)(\V*)(.*)', '${1}' & $_Val & '$3')
                        $s_data = StringTrimLeft($s_data, 2)
                        $s_data = StringTrimRight($s_data, 3)
                        $Write = FileWrite($FO, $s_data)
                Case 1
                        If $s_File <> '' Then $s_File = StringRegExpReplace($s_File, '(\r?\n)*$', '') & @CRLF
                        $Write = FileWrite($FO, $s_File & $data & @CRLF)
                Case 2, 3
                        $s_data = StringRegExpReplace(@CRLF & $s_File & @CRLF & '[', '(?si)(.*?\r?\n\h*' & $_Section & '.*?)((?:\r?\n)+)(\h*\[.*)', '$1' & @CRLF & $_key & '=' & $_Val & '$2$3')        ;131022党淏*峈+
                        $s_data = StringTrimLeft($s_data, 2)
                        $s_data = StringTrimRight($s_data, 3)
                        $Write = FileWrite($FO, $s_data)
        EndSwitch
        FileClose($FO)
        If $Write = 0 Then Return 0
        Return 1
EndFunc   ;==>_IniWrite


; 滲杅靡:_IniWriteSection;=====================================================================
; 佽隴:﹛蔚杅擂迡善INI賦凳恅璃腔珨跺趼僇﹝
;﹛﹛﹛﹛掛滲杅婓迡頗珂勤杅擂輛俴淕燴ㄛ壺拸虴腔瑩硉摯嗣豻腔忑帣諾跡ㄛ奧準埻IniWriteSection
;﹛﹛﹛﹛滲杅祥樓煦昴腔窒迡﹝
; 逄楊:﹛_IniWriteSection('ini恅璃繚噤', '趼僇靡', '杅擂'[, '坰竘'[, 湖羲耀宒]])
; 統杅:﹛褫恁統杅'坰竘'ㄛ森統杅躺婓杅擂峈杅郪奀衄虴, 硌隅羲宎迡腔坰竘ㄛ蘇峈 1﹝
; 殿隙硉:傖髡: 殿隙1﹝
;﹛﹛﹛﹛囮啖: 殿隙0﹝甜扢离 @Error 峈眕狟硉ㄩ
;﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛@Error=1 杅郪準2峎麼菴2峎準2˙
;﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛@Error=2 杅擂跡宒渣昫˙
;﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛@Error=3 恅璃撿衄硐黍扽俶麼氪拸楊迡杅擂扢离﹝
; 蛁砩:﹛彆趼僇眒湔婓ㄛ蔚頗珂諾埻瑩硉﹝
;==========================================================================================start
Func _IniWriteSection($iFile, $Section, $data, $index = 1, $mode = 0)
        Local $_data, $s_data = '', $s_Section = '', $key, $i, $FO, $Write, $CRLF = @CRLF
        If IsArray($data) Then
                If UBound($data, 0) <> 2 Or UBound($data, 2) <> 2 Then Return SetError(1, 0, 0)
                For $i = $index To UBound($data) - 1
                        $key = StringRegExpReplace($data[$i][0], '^\h*([^=;\s].+?)\h*$', '$1')
                        If Not StringRegExp($key, '^\s*$') Then $s_Section &= $key & '=' & StringRegExpReplace($data[$i][1], '^\h*([^=;\s].+?)\h*$', '$1') & @CRLF
                Next
                If $s_Section = '' Then Return SetError(2, 0, 0)
        Else
                $_data = StringRegExp(@CRLF & $data & @CRLF, '\n\h*([^=;\s].*?)\h*\=\h*(.*?)\h*(?=\r?\n)', 3)
                If @error Then Return SetError(2, 0, 0)
                For $i = 0 To UBound($_data) - 1 Step 2
                        $s_Section &= $_data[$i] & '=' & $_data[$i + 1] & @CRLF
                Next
        EndIf
        Local $s_File = _FileRead($iFile, $mode)
        Local $_Section = StringRegExpReplace($Section, '^\h*(.+?)\h*$', '$1')
        $Section = '[' & $_Section & ']' & @CRLF
        $_Section = StringRegExpReplace($_Section, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)', '\\$1')
        $_Section = '\[\h*' & $_Section & '\h*\]'
        $s_data = StringRegExpReplace(@CRLF & $s_File & @CRLF & '[', '(?si)(.*?\r?\n)\h*' & $_Section & '.*?(\[.*)', '${1}' & $Section & $s_Section & '$2')
        If @extended > 0 Then
                $s_data = StringTrimLeft($s_data, 2)
                $s_data = StringTrimRight($s_data, 3)
        Else
                If $s_File = '' Then $CRLF = ''
                $s_data = StringRegExpReplace($s_File, '(\r?\n)*$', '') & $CRLF & $Section & $s_Section
        EndIf
        $FO = FileOpen($iFile, $mode + 2)
        $Write = FileWrite($FO, $s_data)
        FileClose($FO)
        If $Write = 0 Then Return SetError(3, 0, 0)
        Return 1
EndFunc   ;==>_IniWriteSection


; 滲杅靡:_IniReadSection_str;===================================================================
; 佽隴:﹛植(準)梓袧INI賦凳恅璃笢黍議跺趼僇(褫恁笭葩趼僇靡)笢腔垀衄趼睫﹝
; 逄楊:﹛_IniReadSection_str('ini恅璃繚噤', '趼僇靡'[, 湖羲耀宒[, 笭葩梓祩]])
; 統杅:﹛笭葩梓祩 - [褫恁]ㄛ衄虳inf恅璃頗衄笭葩腔趼僇靡ㄛ扢离森梓祩峈1奀褫黍垀衄囀﹝
; 殿隙硉:傖髡: 殿隙趼僇笢腔垀衄趼睫﹝
;﹛﹛﹛﹛囮啖: 殿隙0﹝INI恅璃黍渣昫麼趼僇祥湔婓蔚扢离 @error=1˙趼僇囀峈諾蔚扢离 @error=2
;==========================================================================================start
Func _IniReadSection_str($iFile, $Section, $mode = 0, $nFlag = 0)
        Local $s_File = _FileRead($iFile, $mode), $line_all
        Local $_Section = StringRegExpReplace($Section, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)', '\\$1')
        Local $s_Sec = StringRegExp($s_File & @CRLF & '[', '(?si)(?:^|\n)\h*\[\h*' & $_Section & '\h*\]\V*(.*?)\n\h*(?=\[)', 3)
        If @error Then Return SetError(1, 0, 0)
        $line_all = StringRegExpReplace($s_Sec[0], '^\r?\n|\r$', '')
        If $nFlag Then
                For $i = 1 To UBound($s_Sec) - 1
                        $line_all &= StringRegExpReplace($s_Sec[$i], '^\r?\n|\r$', '')
                Next
        Endif
        If Not StringRegExp($line_all, '\V') Then Return SetError(2, 0, 0)
        Return $line_all
EndFunc   ;==>_IniReadSection_str


; 滲杅靡:_IniReadSection_line;===================================================================
; 佽隴:﹛植(準)梓袧INI賦凳恅璃笢紨俴黍議跺趼僇笢腔趼睫善珨跺1峎杅郪﹝
; 逄楊:﹛_IniReadSection_line('ini恅璃繚噤', '趼僇靡'[, 湖羲耀宒[, 笭葩梓祩]])
; 統杅:﹛笭葩梓祩 - [褫恁]ㄛ衄虳inf恅璃頗衄笭葩腔趼僇靡ㄛ扢离森梓祩峈1奀褫黍垀衄囀﹝
; 殿隙硉:傖髡: 殿隙珨跺1峎杅郪ㄛ藩跺啋匼峈趼僇笢珨俴趼睫﹝
;﹛﹛﹛﹛囮啖: 殿隙0﹝INI恅璃黍渣昫麼趼僇祥湔婓蔚扢离 @error=1˙趼僇囀峈諾蔚扢离 @error=2
; 蛁砩:﹛藩俴趼睫揹腔忑帣諾跡歙頗齬壺˙祥頗殿隙菴珨跺準諾跡趼睫峈※;§腔蛁庋俴﹝
;==========================================================================================start
Func _IniReadSection_line($iFile, $Section, $mode = 0, $nFlag = 0)
        Local $s_Sec = _IniReadSection_str($iFile, $Section, $mode, $nFlag)
        If @error Then Return SetError(@error, 0, 0)
        Local $s_line = StringRegExp($s_Sec, '(?m)^\h*([^;\s]\V*?)\s*$', 3)
        If @error Then Return SetError(2, 0, 0)
        Return $s_line
EndFunc   ;==>_IniReadSection_line


; 滲杅靡:_FileRead;============================================================================
; 佽隴:﹛蚚FileOpen腔湖羲耀宒殿隙黍恅璃腔趼睫揹
;==========================================================================================start
Func _FileRead($iFile, $mode = 0)
        Local $o_File = FileOpen($iFile, $mode)
        Local $s_File = String(FileRead($o_File))
        FileClose($o_File)
        Return $s_File
EndFunc   ;==>_FileRead


; 滲杅靡:_IniDelete_line;=============================================================================
; 佽隴:﹛植(準)梓袧INI賦凳恅璃笢刉壺(漪衄)壽瑩趼腔俴﹝
; 逄楊:﹛_IniDelete_line('ini恅璃繚噤', '趼僇靡', '壽瑩趼'[, 饜梓祩[, 湖羲耀宒]])
; 統杅:﹛褫恁統杅 饜梓祩ㄛ= 0 漪衄猁刉壺腔壽瑩趼(蘇)˙ = 1 俇淕饜壽瑩趼﹝
; 殿隙硉:傖髡: 殿隙1﹝
;﹛﹛﹛﹛囮啖: 殿隙0﹝甜扢离 @Error 峈眕狟硉ㄩ
;﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛@Error=1 恅璃麼趼僇祥湔婓˙
;﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛@Error=3 跦擂饜梓祩帤饜善壽瑩趼(祥湔婓)˙
;﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛﹛@Error=4 恅璃撿衄硐黍扽俶麼氪拸楊迡杅擂扢离﹝
; 蛁砩:﹛蜆滲杅躺硒俴珨棒刉壺俴ㄛ衄嗣俴(漪衄)壽瑩趼腔俴剒猁刉壺ㄛ褫嗣棒硒俴﹝
;==========================================================================================start
Func _IniDelete_line($iFile, $Section, $key, $flag = 0, $mode = 0)
        Local $s_File = _FileRead($iFile, $mode)
        Local $_Section = StringRegExpReplace($Section, '^\h*(.+?)\h*$', '$1')
        $_Section = StringRegExpReplace($_Section, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)', '\\$1')
        $_Section = '\[\h*' & $_Section & '\h*\]'
        If Not StringRegExp(@CRLF & $s_File, '\r?\n\h*' & $_Section & '\s*') Then Return SetError(1, 0, 0)
        Local $_key = StringRegExpReplace($key, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\|\=)', '\\$1')
        If $flag = 0 Then $_key = '[^\r?\n]*?' & $_key & '.*?'
        Local $s_data = StringRegExpReplace(@CRLF & $s_File & @CRLF, '(?si)(.*?\r?\n\h*' & $_Section & '\h*\r?\n(?:\h*[^\[\r\n]*\r?\n)*?)' & $_key & '\r?\n(.*)', '${1}$2')
        If @Extended = 0 Then Return SetError(3, 0, 0)
        $s_data = StringTrimRight(StringTrimLeft($s_data, 2), 2)
        Local $FO = FileOpen($iFile, $mode + 2)
        Local $Write = FileWrite($FO, $s_data)
        FileClose($FO)
        If $Write = 0 Then Return SetError(4, 0, 0)
        Return 1
EndFunc   ;==>_IniDelete_line
发表于 2018-2-1 18:52:23 | 显示全部楼层
你提的这个UDF 是ini文件操作,你把你的txt后缀修改成 ini  再看ini操作的UDF说明就可以了
 楼主| 发表于 2018-2-2 07:52:28 | 显示全部楼层
回复 2# heroxianf


    大大, 我看到的UDF說明是亂碼耶...
 楼主| 发表于 2018-2-2 08:03:38 | 显示全部楼层
本帖最后由 ipmitool 于 2018-2-2 08:47 编辑

回复 2# heroxianf


    大大, 我看到的UDF說明是亂碼耶...
那如果改成這樣呢, 想要show出所有 [xxx] 下面的字 要怎麼做阿...現在show出來怪怪的...
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

Local $path = @ScriptDir&""
Local $aFileList = _FileListToArray($path, "testconfig.txt")


If @error = 1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
        Exit
EndIf
If @error = 4 Then
        MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
        Exit
EndIf
; Display the results returned by _FileListToArray.
_ArrayDisplay($aFileList, "$aFileList")



Local $i, $aRetArray
For $i = 1 To UBound($aFileList) - 1 Step 1
        _FileReadToArray($aFileList[$i], $aRetArray, 1)

        _ArrayDisplay($aRetArray, "$aRetArray")

        Local $j
        For $j = 1 To UBound($aRetArray) - 1
                Local $start = StringInStr($aRetArray[$j], "[")    ;这里是你需要查找的错误字符
                If $start <> "" Then
                        MsgBox(0, "提示", "在文件" & $aFileList[$i] & "中,第" & $j & "行,第" & $start & "个字符" & @CRLF & "有字符[")
                EndIf
        Next
Next
 楼主| 发表于 2018-2-2 08:53:51 | 显示全部楼层
成功了, 謝謝喔~
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

Local $path = @ScriptDir&""
Local $aFileList = _FileListToArray($path, "testconfig.txt")


If @error = 1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
        Exit
EndIf
If @error = 4 Then
        MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
        Exit
EndIf
; Display the results returned by _FileListToArray.
;_ArrayDisplay($aFileList, "$aFileList")



Local $i, $aRetArray
For $i = 1 To UBound($aFileList) - 1 Step 1
        _FileReadToArray($aFileList[$i], $aRetArray, 1)

        _ArrayDisplay($aRetArray, "$aRetArray")

        Local $j
        For $j = 1 To UBound($aRetArray) - 1
                Local $start = StringInStr($aRetArray[$j], "[")    ;这里是你需要查找的错误字符

                If $start <> "" Then
                        MsgBox(0, "提示", "在文件" & $aFileList[$i] & "中,第" & $j & "行,第" & $start & "个字符" & @CRLF & "有字符[")
                                                $Linestring1=FileReadLine("testconfig.txt",$j+1)
                                                $Linestring2=FileReadLine("testconfig.txt",$j+2)
                                                $Linestring3=FileReadLine("testconfig.txt",$j+3)
                                                MsgBox(0, "Msg", $Linestring1,1)
                                                MsgBox(0, "Msg", $Linestring2,1)
                                                MsgBox(0, "Msg", $Linestring3,1)
                EndIf
        Next
Next

评分

参与人数 1金钱 +10 收起 理由
heroxianf + 10 厉害我的哥~

查看全部评分

您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-28 09:33 , Processed in 0.077901 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表