找回密码
 加入
搜索
查看: 5993|回复: 15

[AU3基础] 非标ini文件的写操作[已解决]

[复制链接]
发表于 2021-4-1 22:17:38 | 显示全部楼层 |阅读模式
本帖最后由 redapple2008 于 2021-4-7 08:46 编辑

例如:
[www]
www.ljbaidu.com
www.autoitx.com
[ip]
127.0.0.1
192.168.1.1
像这种的配置文件,怎么修改?删除或者改写操作?
发表于 2021-4-2 00:02:23 | 显示全部楼层
本帖最后由 yuantian 于 2021-4-2 00:10 编辑

IniReadSection()   ??
不对,这个只是读取、、、

你可以学习一下A版大大改进的INI的UDF

代码放上来容易丢, 你看这里吧
https://www.autoitx.com/thread-12319-1-1.html
 楼主| 发表于 2021-4-2 12:22:20 来自手机 | 显示全部楼层
是啊!都到了,想想怎么删写?
发表于 2021-4-4 10:18:07 | 显示全部楼层
redapple2008 发表于 2021-4-2 12:22
是啊!都到了,想想怎么删写?

读取显示在GUICtrlCreateEdit控件后修改

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2021-4-4 20:40:19 | 显示全部楼层
chzj589 发表于 2021-4-4 10:18
读取显示在GUICtrlCreateEdit控件后修改

怎么删除?或者改写?
发表于 2021-4-4 21:29:14 | 显示全部楼层
redapple2008 发表于 2021-4-4 20:40
怎么删除?或者改写?

编辑框里修改或添加后保存
 楼主| 发表于 2021-4-4 21:39:52 | 显示全部楼层
chzj589 发表于 2021-4-4 21:29
编辑框里修改或添加后保存

有代码吗?给我参考一下,谢谢!
发表于 2021-4-5 01:09:27 | 显示全部楼层
先将非标准ini转换成内置函数可以处理的格式。
处理完毕后再转换回去。
可以这样:
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
Global $FilePath = @ScriptDir&"\setup.ini"
 If FileExists($FilePath) Then FileDelete($FilePath)
;写入实例
IniWrite($FilePath, "www", "www.ljbaidu.com", "")
IniWrite($FilePath, "www", "www.autoitx.com", "")
IniWrite($FilePath, "ip", "127.0.0.1", "")
IniWrite($FilePath, "ip", "192.168.1.1", "")



_ini_to_file($FilePath)
;显示非标准ini实例
ShellExecute($FilePath)
Sleep(2000)
_file_to_ini($FilePath);转换到标准
IniDelete($FilePath, "www", "www.ljbaidu.com")
IniDelete($FilePath, "ip", "192.168.1.1")
IniWrite($FilePath, "ip", "192.168.100.1", "")
IniWrite($FilePath, "www", "www.163.com", "")
IniWrite($FilePath, "www", "www.qq.com", "")
_ini_to_file($FilePath);转换到非标准
ShellExecute($FilePath)

Func _file_to_ini($sFilePath)
 If Not FileExists($sFilePath) Then Return False
    Local $aArray = FileReadToArray($sFilePath)
    Local $iLineCount = @extended
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "读取文件时出现错误. @error: " & @error) ; 读取当前脚本文件时发生错误.
    Else
        For $i = 0 To $iLineCount - 1 ; 遍历数组. 也可以使用 UBound($aArray).
                        $temp = StringStripWS ( $aArray[$i], 3 )
                        If $temp <> "" Then
                        If StringLeft ( $temp, 1 ) == "[" and StringRight ( $temp, 1 ) == "]" Then
                        Else
                                $aArray[$i] &= "="
                                EndIf
                        EndIf
        Next
                 If FileExists($sFilePath) Then FileDelete($sFilePath)
                 For $i = 0 To $iLineCount - 1
                FileWriteLine($sFilePath, $aArray[$i])
                Next
    EndIf
EndFunc   ;==>Example

Func _ini_to_file($sFilePath)
 If Not FileExists($sFilePath) Then Return False
    Local $aArray = FileReadToArray($sFilePath)
    Local $iLineCount = @extended
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "读取文件时出现错误. @error: " & @error) ; 读取当前脚本文件时发生错误.
    Else
        For $i = 0 To $iLineCount - 1 ; 遍历数组. 也可以使用 UBound($aArray).
                        $temp = StringStripWS ( $aArray[$i], 3 )
                        If $temp <> "" Then
                        If StringLeft ( $temp, 1 ) == "[" and StringRight ( $temp, 1 ) == "]" Then
                        Else
                                If StringRight ( $aArray[$i], 1 ) == "=" Then   $aArray[$i] = StringTrimRight ( $aArray[$i], 1 )
                                EndIf
                        EndIf
        Next
                 If FileExists($sFilePath) Then FileDelete($sFilePath)
                 For $i = 0 To $iLineCount - 1
                FileWriteLine($sFilePath, $aArray[$i])
                Next
    EndIf
EndFunc   ;==>Example

发表于 2021-4-5 09:12:46 | 显示全部楼层
本帖最后由 chzj589 于 2021-4-5 09:17 编辑
redapple2008 发表于 2021-4-4 21:39
有代码吗?给我参考一下,谢谢!
[/pre]既然j是非标ini文件,就用读取写入修改删除TXT操作,读取显示编辑框,写入修改删除后保存[pre]
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
Opt("GUIOnEventMode", 1)
Global $Form1, $Edit1
Example()
While 1
        Sleep(1000)
WEnd
Func Example()
        $Form1 = GUICreate("Form1", 270, 264, -1, -1);193, 125)
        GUISetOnEvent($GUI_EVENT_CLOSE, "_Form1Close")
        ;$Input1 = GUICtrlCreateInput('', 10, 18, 215, 21)
        $Button1 = GUICtrlCreateButton("保 存", 150, 215, 75, 23)
        GUICtrlSetOnEvent($Button1, "_Button1")
        $Edit1 = GUICtrlCreateEdit("", 10, 18, 249, 190);, $ES_READONLY)
        GUISetState(@SW_SHOW, $Form1)
        _Edit1()
EndFunc   ;==>Example
Func _Form1Close()
        GUIDelete()
        Exit
EndFunc   ;==>_Form1Close
Func _Button1()
        _AddFilec()
EndFunc   ;==>_Button1
Func _Edit1();添加读取
        If FileExists(@ScriptDir & "\22.ini") Then
                $sTexts = @ScriptDir & "\22.ini"
                GUICtrlSetData($Edit1, Readtxte(), "")
        Else
                MsgBox(4096, "文件", "22.ini 不文件存在")
        EndIf
EndFunc   ;==>_Edit1
Func Readtxte()
        $sTexts = @ScriptDir & "\22.ini"
        $SEARCHa = $sTexts
        Global $Files = $SEARCHa
        Local $txt, $temp
        $temp = FileOpen($Files, 0)
        $txt = FileRead($temp)
        FileClose($temp)
        Return $txt
        GUICtrlSetData($Edit1, $temp)
EndFunc   ;==>Readtxte
Func _AddFilec();另存
        Readtxteab();
        $yj1 = GUICtrlRead($Edit1)
        $var = @ScriptDir & "\22.ini"
        FileDelete($var)
        $File = FileOpen($var, 1)
        FileWrite($File, "" & $yj1 & @CRLF)
        FileClose($File)
        ShellExecute($var)
        _Edit1()
EndFunc   ;==>_AddFilec
Func Readtxteab()
        $sTexts = GUICtrlRead($Edit1)
        $SEARCH = $sTexts
        Global $Files = $sTexts
        Local $txt, $temp
        $temp = FileOpen($Files, 0)
        $txt = FileRead($temp)
        FileClose($temp)
        Return $txt
EndFunc   ;==>Readtxteab

 楼主| 发表于 2021-4-6 16:28:49 | 显示全部楼层
十分感谢上面两位,我先研究一下
发表于 2021-4-6 20:38:15 | 显示全部楼层


刚动了下脑,我也来份我实现的UDF吧。

     利用 分割和替换 的方法

#include <String.au3>
#include <array.au3>


;读取 非标文件中的  字段名
Func _oIniReadName($_md)
        Local $str = FileRead($_md);读取文件
        If @extended < 2 Then Return -2 ;读取文件失败
        Local $arr = _StringBetween($str, '[', ']');取两字符 串间的内容
        Return $arr ;返回一个基于0开始计算的一维数组, $array[0] 包含第一个找到的内容.
EndFunc   ;==>_oIniReadName

;读取 非标文件中的 所有的键
Func _oIniReadALL($_md, $_name)
        Local $str = FileRead($_md);读取文件
        If @extended < 2 Then Return -2 ;读取文件失败
        Local $arr = _StringBetween($str & '[', '[' & $_name & ']', '[') ;取两字符 串间的内容
        If $arr = 0 Then Return 0;返回0 获取 失败
        $str = StringRegExpReplace($arr[0], '^\v+|\v+(?=\r\n\V|$)', '') ;清掉空行
        Return StringSplit($str, @CRLF, 1) ;返回 一个所有键的数组
EndFunc   ;==>_oIniReadALL

;删除 非标文件中的 字段名
Func _oIniDelName($_md, $_name)
        Local $str = FileRead($_md);读取文件
        If @extended < 2 Then Return -2 ;读取文件失败
        Local $arr = _StringBetween($str & '[', '[' & $_name & ']', '[') ;取两字符 串间的内容
        If $arr = 0 Then Return 0;返回0 没有这个字段 名
        $str = StringReplace($str, '[' & $_name & ']' & $arr[0], "")
        Local $file = FileOpen($_md, 2)
        FileWrite($file, $str)
        FileClose($file)
        Return ;
EndFunc   ;==>_oIniDelName


;删除或添加 非标文件中的 键
;参数 $_md 文件
;        $_name 字段名
;   $_var 键名
;  $add = 0 添加 键名, = 1 删除键名
Func _oIniDelNameVer($_md, $_name, $_var, $add = 0) 
        Local $str = FileRead($_md);读取文件
        If @extended < 2 Then Return -2 ;读取文件失败
        Local $arr = _StringBetween($str & '[', '[' & $_name & ']', '[') ;取两字符 串间的内容
        If $arr = 0 Then Return 0;返回0 没有这个字段 名
        If $add = 0 Then ;删除
                Local $strl = StringReplace($arr[0] & @CRLF, @CRLF & $_var & @CRLF, @CRLF)
                If @extended < 1 Then Return -1;返回 -1 没有这个键
        Else ;$add = 其它值  添加
                Local $strl = StringReplace($arr[0] & @CRLF, @CRLF & $_var & @CRLF, @CRLF)
                If @extended > 0 Then Return -11;返回 -11 已有这个键
                $strl = $arr[0]&@crlf&$_var&@CRLF ;这里漏掉一个回车。
        EndIf
        $str = StringReplace($str, '[' & $_name & ']' & $arr[0], '[' & $_name & ']' & $strl)
        Local $file = FileOpen($_md, 2)
        FileWrite($file, StringRegExpReplace($str, '^\v+|\v+(?=\r\n\V|$)', ''));清掉空行
        FileClose($file)
        Return 1;成功返回1
EndFunc   ;==>_oIniDelNameVer

 

 楼主| 发表于 2021-4-7 08:45:39 | 显示全部楼层
绿色风 发表于 2021-4-6 20:38
刚动了下脑,我也来份我实现的UDF吧。

     利用 分割和替换 的方法

这个写的很祥细,解决了我的问题。注解也不错。谢谢了。
发表于 2021-4-7 12:10:28 | 显示全部楼层
绿色风 发表于 2021-4-6 20:38
刚动了下脑,我也来份我实现的UDF吧。

     利用 分割和替换 的方法

不错。测试发现多个字段名添加键名会出现下个字段名与键名合在一起

评分

参与人数 1金钱 +50 贡献 +1 收起 理由
绿色风 + 50 + 1 已解决

查看全部评分

发表于 2021-4-7 15:32:15 | 显示全部楼层
chzj589 发表于 2021-4-7 12:10
不错。测试发现多个字段名添加键名会出现下个字段名与键名合在一起

......
      Else ;$add = 其它值  添加
                Local $strl = StringReplace($arr[0] & @CRLF, @CRLF & $_var & @CRLF, @CRLF)
                If @extended > 0 Then Return -11;返回 -11 已有这个键
                $strl = $arr[0]&@crlf&$_var&@CRLF ;这里漏掉一个回车。 加上就OK
        EndIf
...
发表于 2021-4-7 16:46:33 | 显示全部楼层
绿色风 发表于 2021-4-7 15:32
......
      Else ;$add = 其它值  添加
                Local $strl = StringReplace($arr[0] & @C ...

是的,我在
$str = StringReplace($str, '[' & $_name & ']' & $arr[0], '[' & $_name & ']' & $strl & @CRLF)
;加一个回车。 OK
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-4-24 19:45 , Processed in 0.083506 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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