2013-07-21 2 views
3

Inno Setup을 사용하여 텍스트 파일에서 특정 라인을 편집해야합니다. 설치 프로그램에서이 줄 ("appinstalldir" "C:MYXFOLDER\\apps\\common\\App70")을 찾고 설치 관리자의 디렉터리 경로를 사용해야합니다.Inno Setup - 설치하는 동안 텍스트 파일에서 특정 라인을 편집하는 방법?

procedure CurStepChanged(CurStep: TSetupStep); 
begin 
    if CurStep = ssDone then 
    begin 
    SaveStringToFile(
     ExpandConstant('{app}\app70.txt'), 
     'directory's path' + '\\apps\\common\\App70', True); 
    end; 
end; 

이 내 텍스트 파일입니다 :

"App" 
{ 
    "appID"  "70" 

    { 
     "appinstalldir"  "C:MYXFOLDER\\apps\\common\\App70" 
    } 
} 
+0

해당 파일을 쓰는 모든 것이 매우 엄격한 지 확인하십시오. '{'또는'} '을 같은 행에두면 파서가 깨집니다. – Deanna

답변

8

이 코드는 그것을 할 수

내가 사용하려고하고있는 코드입니다. 그러나이 코드는 태그의 값이 따옴표로 묶인 경우 TagName 매개 변수로 지정된 태그를 찾으면 나머지 줄을 잘라내어 TagValue 매개 변수로 지정된 값을 추가합니다.

function ReplaceValue(const FileName, TagName, TagValue: string): Boolean; 
var 
    I: Integer; 
    Tag: string; 
    Line: string; 
    TagPos: Integer; 
    FileLines: TStringList; 
begin 
    Result := False; 
    FileLines := TStringList.Create; 
    try 
    Tag := '"' + TagName + '"'; 
    FileLines.LoadFromFile(FileName); 
    for I := 0 to FileLines.Count - 1 do 
    begin 
     Line := FileLines[I]; 
     TagPos := Pos(Tag, Line); 
     if TagPos > 0 then 
     begin 
     Result := True; 
     Delete(Line, TagPos + Length(Tag), MaxInt); 
     Line := Line + ' "' + TagValue + '"'; 
     FileLines[I] := Line; 
     FileLines.SaveToFile(FileName); 
     Break; 
     end; 
    end; 
    finally 
    FileLines.Free; 
    end; 
end; 

procedure CurStepChanged(CurStep: TSetupStep); 
var 
    NewPath: string; 
begin 
    if CurStep = ssDone then 
    begin 
    NewPath := ExpandConstant('{app}') + '\apps\common\App70'; 
    StringChangeEx(NewPath, '\', '\\', True); 

    if ReplaceValue(ExpandConstant('{app}\app70.txt'), 'appinstalldir', 
     NewPath) 
    then 
     MsgBox('Tag value has been replaced!', mbInformation, MB_OK) 
    else 
     MsgBox('Tag value has not been replaced!.', mbError, MB_OK); 
    end; 
end; 
+0

안녕하세요, TLama 님,이 코드를 편집했습니다 : ReplaceValue (ExpandConstant ('{app} \ app70.txt'), 'appinstalldir', ExpandConstant ('{app}') + '\\ apps \\ common \\ App70 ')가' 내가 txt 파일에서 좋은 결과를 얻었다 : "앱" { "APPID", "70" { "appinstalldir" "C : \ Program 파일 (x 86) \ FOLDER1 \ FOLDER2 \\ apps \\ common \\ App70 " } } 하지만이 문제가 있습니다 ... 내 소프트웨어는 다음 형식을 가진 경우"appinstalldir "디렉토리 경로 만 읽습니다 : ''C : \\ Program Files (x86) \\ FOLDER1 \\ FOLDER2 \\ apps \\ common \\ App70 "'추가"\ "를 추가해야합니까, 어떻게 할 수 있습니까? – Dielo

+1

방금 ​​의견을 편집했습니다. 확인해주세요. – Dielo

+1

알겠습니다. 새 값의'ExpandConstant ('{app}')'부분에 대해 백 슬래시를 두 배로 늘려야합니다. 이렇게하기위한 직접 함수가 없으므로, 특정 문자열 (또는이 경우 char)의 모든 발생을 다른 것으로 대체하는'StringChangeEx' 함수를 사용할 수 있습니다. 귀하의 경우에는 단일 백 슬래시로 경로를 구축 한 다음이 문자열에서 편집 된 코드에 표시된 것처럼 백 슬래시를 이중 백 슬래시로 교체하십시오. – TLama

관련 문제