{对Unicode文件进行替换}
function TForm22.SearchIniFile(const FileName: string;const SearchText: string;
IsReplace: Boolean;const ReplaceText: string): boolean;
var
getStream: TFileStream; {声明一个文件流}
setStream: TFileStream;
getPath: String;
meint, I, J: LongInt;
pstr: Pchar;
mestr, mestr1: String;
bool: boolean;
begin
getPath := FileName;
getStream := TFileStream.Create(getPath, fmOpenRead);
meint := getStream.Size; {得到文件长度}
getmem(pstr, meint+2);//申请字符指针内存
fillchar(pstr^, meint+2, 0); {清除内存中原有的内容}
I := getStream.Read(pstr^, meint); //在流里读出meint大小的字符串到字符指针
mestr := strpas(pstr); //字符指针转成字符
freemem(Pstr); {释放}
getStream.Free; {关闭}
if PosEx(SearchText, mestr, 1) > 0 then {表示inString中有一句话SearchText}
if Pos(SearchText, mestr) > 0 then
begin
bool := true;
fileNum := fileNum + 1;
bl := true;
if IsReplace then
begin
mestr1 := StringReplace(mestr, SearchText, ReplaceText, [rfReplaceAll]);
setStream := TFileStream.Create(getPath, fmCreate);
J := length(mestr1) * 2; //字符长度
pstr := Pchar(mestr1);//把字符串转成字符指针
setStream.Write(pstr^, J);//把字符串写入流中
setStream.Free;
ProgressBar1.Position := ProgressBar1.Position + 1;
end;
end
else
begin
bool := false;
end;
StatusBar1.Panels[0].Text := '找到 ' + IntToStr(fileNum) + ' 个文件';
result := bool;
end;
转载于:https://www.cnblogs.com/zousheng7018/archive/2012/10/17/2727227.html