在Inno Setup中用Pascal脚本从配置文件中查找并读取特定的字符串 [英] Find and read specific string from config file with Pascal Script in Inno Setup
本文介绍了在Inno Setup中用Pascal脚本从配置文件中查找并读取特定的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要提取/读取的是特定号码位置的InstallDir。为20540.
我知道如何在INI或XML中查找字符串,但无法处理这种形式的文件。
<
212280
{
InstallDir D:\\\\\\\\\\\\\\\''''''''
UpdateB更新0
HasAllLocalContent1
UpToDate1
DisableAutoUpdate0
}
20540
{
UpdateKBtoDL0
InstallDirC:\\ABC \\def\\ghi
HasAllLocalContent1
UpToDate1
maintenance_time1339663134
DisableAutoUpdate0
4560
{
UpdateKBtoDL0
HasAllLocalContent0
UpToDate0
InstallDir
解决方案
你需要编写你自己的解析器。这可能是一个可能的实现:
[code]
函数GetInstallDir(const FileName,Section:string):string ;
var
S:string;
DirLine:Integer;
LineCount:Integer;
SectionLine:Integer;
行:TArrayOfString;
begin
结果:='';
S:=''+ Section +''; // AddQuotes被打断了...
如果LoadStringsFromFile(FileName,Lines)那么
begin
LineCount:= GetArrayLength(Lines);如果(SectionLine< LineCount)和(Trim(Lines,SectionLine))= S,那么
开始
,则
为SectionLine:= 0到LineCount - 1做
。 (SectionLine + 1))='{')then
for DirLine:= SectionLine to LineCount - 1 do
begin
if(Pos('InstallDir',Lines [DirLine]) > 0)和
(StringChangeEx(Lines [DirLine],''InstallDir'','',True)> 0),则
开始
S:= RemoveQuotes(Trim [DirLine]));
StringChangeEx(S,'\\','\',True);
结果:= S;
退出;
end;
如果修剪(Lines [DirLine])='}'则
退出;
end;
退出;
end;
end;
end;
程序InitializeWizard;
begin
MsgBox(GetInstallDir('d:\File.almostjson','20540'),mbInformation,MB_OK);
end;
I have quite long config file and I need to extract specific strings from the file. What I want to extract/read is InstallDir for specific number position e.g. for 20540.
I know how to find string in INI or XML, but cannot handle this form of file.
Piece of the file that shows structure:
"212280"
{
"InstallDir" "D:\\XYZ\\stu\\opr"
"UpdateKBtoDL" "0"
"HasAllLocalContent" "1"
"UpToDate" "1"
"DisableAutoUpdate" "0"
}
"20540"
{
"UpdateKBtoDL" "0"
"InstallDir" "C:\\ABC\\def\\ghi"
"HasAllLocalContent" "1"
"UpToDate" "1"
"maintenance_time" "1339663134"
"DisableAutoUpdate" "0"
}
"4560"
{
"UpdateKBtoDL" "0"
"HasAllLocalContent" "0"
"UpToDate" "0"
"InstallDir" ""
}
解决方案
You'll need to write your own parser. This might be one possible implementation:
[Code]
function GetInstallDir(const FileName, Section: string): string;
var
S: string;
DirLine: Integer;
LineCount: Integer;
SectionLine: Integer;
Lines: TArrayOfString;
begin
Result := '';
S := '"' + Section + '"'; // AddQuotes is broken somehow...
if LoadStringsFromFile(FileName, Lines) then
begin
LineCount := GetArrayLength(Lines);
for SectionLine := 0 to LineCount - 1 do
if Trim(Lines[SectionLine]) = S then
begin
if (SectionLine < LineCount) and (Trim(Lines[SectionLine + 1]) = '{') then
for DirLine := SectionLine to LineCount - 1 do
begin
if (Pos('"InstallDir"', Lines[DirLine]) > 0) and
(StringChangeEx(Lines[DirLine], '"InstallDir"', '', True) > 0) then
begin
S := RemoveQuotes(Trim(Lines[DirLine]));
StringChangeEx(S, '\\', '\', True);
Result := S;
Exit;
end;
if Trim(Lines[DirLine]) = '}' then
Exit;
end;
Exit;
end;
end;
end;
procedure InitializeWizard;
begin
MsgBox(GetInstallDir('d:\File.almostjson', '20540'), mbInformation, MB_OK);
end;
这篇关于在Inno Setup中用Pascal脚本从配置文件中查找并读取特定的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文