本文介绍了在Inno Setup中用Pascal脚本从配置文件中查找并读取特定的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有很长的配置文件,我需要从文件中提取特定的字符串。 我想要提取/读取的是特定号码位置的InstallDir。为20540. 我知道如何在INI或XML中查找字符串,但无法处理这种形式的文件。 212280 {InstallDir D:\\\\\\\\\\\\\\\''''''''UpdateB更新0HasAllLocalContent1UpToDate1DisableAutoUpdate0} 20540 {UpdateKBtoDL0InstallDirC:\\ABC \\def\\ghiHasAllLocalContent1UpToDate1maintenance_time1339663134DisableAutoUpdate0 4560 {UpdateKBtoDL0HasAllLocalContent0UpToDate0InstallDir 解决方案你需要编写你自己的解析器。这可能是一个可能的实现: [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脚本从配置文件中查找并读取特定的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 23:27