2015-08-21 28 views
0

我有一段代碼合併兩個文本文件(第一個在第二個)。如何檢查第一個文件的內容是否在第二個文件中,然後跳過合併?Inno Setup:比較兩個文件的內容


[Code] 
procedure AppendFile(const SrcFile, DestFile: string); 
var 
    SrcStream: TFileStream; 
    DestStream: TFileStream; 
begin 
    SrcStream := TFileStream.Create(SrcFile, fmOpenRead); 
    try 
    DestStream := TFileStream.Create(DestFile, fmOpenWrite); 
    try 
     DestStream.Seek(0, soFromEnd); 
     DestStream.CopyFrom(SrcStream, SrcStream.Size); 
    finally 
     DestStream.Free; 
    end; 
    finally 
    SrcStream.Free; 
    end; 
end; 
+0

我在哪裏可以找到個例? –

+0

你使用什麼合併代碼?它是Inno安裝Pascal腳本代碼嗎?你可以把它展示給我們嗎?或者你使用外部工具? –

+0

http://pastebin.com/Qg77yC3T這是我使用的代碼:) –

回答

1

最簡單的Unicode安全的實現是利用TStringList class

function NeedAppend(const SrcFile, DestFile: string): Boolean; 
var 
    SrcContents: TStringList; 
    DestContents: TStringList; 
begin 
    SrcContents := TStringList.Create(); 
    DestContents := TStringList.Create(); 
    try 
    SrcContents.LoadFromFile(SrcFile); 
    DestContents.LoadFromFile(DestFile); 

    Result := (Pos(SrcContents.Text, DestContents.Text) = 0); 
    finally 
    SrcContents.Free; 
    DestContents.Free; 
    end; 

    if not Result then 
    begin 
    Log('Contents present already, will not append'); 
    end 
    else 
    begin 
    Log('Contents not present, will append'); 
    end; 
end; 

雖然不是非常有效,如果文件比較大。


一旦你的版本比較這種方式來實現,你可以結合起來,與合併,這個簡單的代碼:

procedure AppendFileIfNeeded(const SrcFile, DestFile: string); 
var 
    SrcContents: TStringList; 
    DestContents: TStringList; 
begin 
    SrcContents := TStringList.Create(); 
    DestContents := TStringList.Create(); 
    try 
    SrcContents.LoadFromFile(SrcFile); 
    DestContents.LoadFromFile(DestFile); 

    if Pos(SrcContents.Text, DestContents.Text) > 0 then 
    begin 
     Log('Contents present already, will not append'); 
    end 
     else 
    begin 
     Log('Contents not present, will append'); 

     DestContents.AddStrings(SrcContents); 
     DestContents.SaveToFile(DestFile); 
    end; 
    finally 
    SrcContents.Free; 
    DestContents.Free; 
    end; 
end; 
+1

最簡單的,如果這是關於僅ANSI文件,那麼我會打電話最簡單(少代碼行,效率類似,也許更好一點)使用'LoadStringFromFile'和'SaveStringToFile'並將'Append'參數設置爲True。 – TLama

+0

是的。我實際上避免了'LoadStringFromFile',因爲它存在ANSI文件的問題。 –