IniRead, OutputVar, Filename, Section, Key [, Default] iniread只能读取指定section指定key值。在不知道section名称和key名称的情况下如何循环读取所有的值?
举报|2014-05-31 14:57
提问者采纳
FileSelectFile, file, , , 请选择ini文件, ini配置文件(*.ini) ;选择文件
if
file=
{
MsgBox,
0
, 错误, 您为选择任何文件
Reload
}
sections =
FileRead, filecontent, %file% ;加载文件到变量
StringSplit, line, filecontent, `n, , ;用函数分割变量为数组
Loop ;循环
{
if
A_Index > %line0%
Break
content = % line%A_Index% ;赋值当前行
StringReplace, content, content, `r, , All ;替换特殊字符
FSection := RegExMatch(content,
"\[.*\]"
) ;正则表达式匹配section
if
FSection =
1
;如果找到
{
TSection := RegExReplace(content,
"\[(.*)\]"
,
"$1"
) ;正则替换并赋值临时section $为向后引用
sections = %sections%%TSection%| ;用|串接所有section 方便使用列出
%TSection%keys =
}
Else
{
FKey := RegExMatch(content,
".*=.*"
) ;正则表达式匹配key
if
FKey =
1
{
TKey := RegExReplace(content,
"(.*)=.*"
,
"$1"
) ;正则替换并赋值临时key
StringReplace, TKey, TKey, ., _, All
TKey2=%Tkey%|
%TSection%keys = % %TSection%keys TKey2 ;;用|串接当前section下所有key 方便使用列出
TValue := RegExReplace(content,
".*=(.*)"
,
"$1"
) ;正则替换并赋值临时value
%TSection%_%TKey%=%TValue%
}
}
}
ListVars ;调试情况 列出所有变量
Pause
|
需要再把串起来的section\key分割成数组请使用Stringsplit命令.
原文摘自:http://zhidao.baidu.com/link?url=hwTXNFFnSKHwstBo15cYy6z6zsBIQpUYtepNNg_ifW_Rpl1XtVnTF9qb5oVFRHG6gXOXPAIVZ56LiZbuooAY0dgt69eE0dOmnLKNtrkTay3