delphi 判断并删除utf8文本文件中的前三个字节BOM

{-------------------------------------------------------------------------------
过程名:    DelBomFromUtf8File 判断并删除utf8文本文件中的前三个字节BOM
参数:      filename:string   1.文件名
返回值:    boolean   是否删除

-------------------------------------------------------------------------------}
function DelBomFromUtf8File(filename:string):boolean;
const
  UTF8_FLAG=$EFBB;
var
  fsRead,fsWrite:TFileStream;
  w:word;
  b:byte;
  filename_tmp:string;
  len:integer;
function WordLoHiExchange(w: Word): Word; register;
asm
   XCHG AL, AH
end;
begin
  result:=false;
  if(fileexists(filename)=false)then exit;
  fsRead:=TFileStream.Create(filename, fmOpenRead or fmShareDenyNone); // or fmOpenWrite
try
  fsRead.Seek(0, soFromBeginning);
  fsRead.Read(w,2);
  w:= WordLoHiExchange(w);
  if w=UTF8_FLAG then
  begin
    fsRead.Read(b,1);
    len:=fsRead.Size-fsRead.Position;
    filename_tmp:=filename+'tmp';
    fsWrite:=TFileStream.Create(filename_tmp, fmCreate);
    fsWrite.CopyFrom(fsRead, len);
    fsWrite.Free;
    result:=true;
  end;
finally
  fsRead.Free;
  if(result)then
  begin
    deletefile(filename);
    movefile(pchar(filename_tmp),pchar(filename));
  end;
end;

{-------------------------------------------------------------------------------
过程名:    RenameDir 重命名文件夹
参数:      fromName,toName:string   1.旧名 2.新名  全路径
返回值:    boolean  是否成功
-------------------------------------------------------------------------------}
function TReplaceString.RenameDir(const fromName,toName:string): boolean;
var
  fo: TSHFILEOPSTRUCT;
begin
  FillChar(fo,SizeOf(fo),0);
  with  fo  do
  begin
    Wnd   :=   0;
    wFunc   :=   FO_RENAME;
    pFrom   :=   PChar(fromName+#0);
    pTo   :=   pchar(toName+#0);
    fFlags   :=   FOF_NOCONFIRMATION+FOF_SILENT;
  end;
  Result   :=   (SHFileOperation(fo)   =   0);
end;


{-------------------------------------------------------------------------------
过程名:    MakeFileList 遍历文件夹及子文件夹
参数:      Path,FileExt:string   1.需要遍历的目录 2.要遍历的文件扩展名
返回值:    TStringList

USE StrUtils

   Eg:ListBox1.Items:= MakeFileList( 'E:\a','.exe') ;
       ListBox1.Items:= MakeFileList( 'E:\a','.*') ;
-------------------------------------------------------------------------------}
function MakeFileList(Path,FileExt:string):TStringList ;
var
sch:TSearchrec;
begin
Result:=TStringlist.Create;

if rightStr(trim(Path), 1) <> '\' then
    Path := trim(Path) + '\'
else
    Path := trim(Path);

if not DirectoryExists(Path) then
begin
    Result.Clear;
    exit;
end;

if FindFirst(Path + '*', faAnyfile, sch) = 0 then
begin
    repeat
       Application.ProcessMessages;
       if ((sch.Name = '.') or (sch.Name = '..')) then Continue;
       if DirectoryExists(Path+sch.Name) then
       begin
         Result.AddStrings(MakeFileList(Path+sch.Name,FileExt));
       end
       else
       begin
         if (UpperCase(extractfileext(Path+sch.Name)) = UpperCase(FileExt)) or (FileExt='.*') then
         Result.Add(Path+sch.Name);
       end;
    until FindNext(sch) <> 0;
    SysUtils.FindClose(sch);
end;
end;

function replaceStr(oldstr,newstr:string;var str:string):boolean;
begin
  result:=false;
  if(pos(oldstr,str)>0)then
  begin
    str:=StringReplace (str, oldstr,newstr, [rfReplaceAll]);
    result:=true;
  end;
end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值