加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

德尔福的文件掩码

发布时间:2020-12-15 09:38:32 所属栏目:大数据 来源:网络整理
导读:我试图找到所有具有扩展名.cbr或.cbz的文件 如果我将我的面具设置为* .cb? 它找到* .cbproj文件.如何将掩码设置为仅查找.cbr和.cbz文件? 这是我正在使用的代码. 我有两个编辑框EDIT1是搜索的位置,EDIT2是我把我的面具放在哪里.用于显示找到内容的列表框和
我试图找到所有具有扩展名.cbr或.cbz的文件

如果我将我的面具设置为* .cb?

它找到* .cbproj文件.如何将掩码设置为仅查找.cbr和.cbz文件?

这是我正在使用的代码.

我有两个编辑框EDIT1是搜索的位置,EDIT2是我把我的面具放在哪里.用于显示找到内容的列表框和“搜索”按钮.

edit1 := c:
edit2 := mask (*.cb?)

空间

procedure TFAutoSearch.FileSearch(const PathName,FileName : string; const InDir : boolean);
var Rec  : TSearchRec;
    Path : string;
begin
Path := IncludeTrailingBackslash(PathName);
if FindFirst(Path + FileName,faAnyFile - faDirectory,Rec) = 0 then
 try
   repeat
     ListBox1.Items.Add(Path + Rec.Name);
   until FindNext(Rec) <> 0;
 finally
   FindClose(Rec);
 end;

If not InDir then Exit;

if FindFirst(Path + '*.*',faDirectory,Rec) = 0 then
 try
   repeat
    if ((Rec.Attr and faDirectory) <> 0)  and (Rec.Name<>'.') and (Rec.Name<>'..') then
     FileSearch(Path + Rec.Name,FileName,True);
   until FindNext(Rec) <> 0;
 finally
   FindClose(Rec);
 end;
end; //procedure FileSearch



procedure TFAutoSearch.Button1Click(Sender: TObject);
begin
  FileSearch(Edit1.Text,Edit2.Text,CheckBox1.State in [cbChecked]);
end;

end.

解决方法

最简单的方法是对当前文件名使用ExtractFileExt,并检查它是否与您想要的扩展名匹配.

这是FileSearch例程的完全重写版本,它正是您正在尝试做的事情(无论如何,根据您的问题):

procedure TFAutoSearch.FileSearch(const ARoot: String);
var
  LExt,LRoot: String;
  LRec: TSearchRec;
begin
  LRoot := IncludeTrailingPathDelimiter(ARoot);
  if FindFirst(LRoot + '*.*',faAnyFile,LRec) = 0 then
  begin
    try
      repeat
        if (LRec.Attr and faDirectory <> 0) and (LRec.Name <> '.') and (LRec.Name <> '..') then
          FileSearch(LRoot + LRec.Name)
        else
        begin
          LExt := UpperCase(ExtractFileExt(LRoot + LRec.Name));
          if (LExt = '.CBR') or (LExt = '.CBZ') then
            ListBox1.Items.Add(LRoot + LRec.Name);
        end;
      until (FindNext(LRec) <> 0);
    finally
      FindClose(LRec);
    end;
  end;
end;

虽然另一个答案暗示使用多个扩展作为掩码* .cbr; * .cbz应该(原则上无论如何)工作,但我通过痛苦的经验注意到Delphi中的FindFirst和FindNext方法往往不接受多个扩展.一张面具!

我提供的代码应该可以满足您的需求,所以尽情享受吧!

更新:允许在运行时动态地在Mask中使用多个扩展(如OP对此答案的第一个评论所示).

我们要做的是从你的TEdit控件中取一个String(这个String是你想要的一个或多个File Extensions),将“爆炸”到一个数组中,然后将每个文件与数组中的每个扩展相匹配.

听起来比这更复杂:

type
  TStringArray = Array of String; // String Dynamic Array type...

// Now let's provide a "Mask Container" inside the containing class...
  TFAutoSearch = class(TForm)
    // Normal stuff in here
  private
    FMask: TStringArray; // Our "Mask Container"
  end;

此代码将填充FMask,每个单独的掩码扩展名由a分隔;例如.CBR; .CBZ.

请注意,此方法不接受通配符或任何其他正则表达式魔法,但您可以根据需要进行修改!

procedure TFAutoSearch.ExplodeMask(const AValue: String);
var
  LTempVal: String;
  I,LPos: Integer;
begin
  LTempVal := AValue;
  I := 0;
  while Length(LTempVal) > 0 do
  begin
    Inc(I);
    SetLength(FMask,I);
    LPos := Pos(';',LTempVal);

    if (LPos > 0) then
    begin
      FMask[I - 1] := UpperCase(Copy(LTempVal,LPos - 1));
      LTempVal := Copy(LTempVal,LPos +  1,Length(LTempVal));
    end
    else
    begin
      FMask[I - 1] := UpperCase(LTempVal);
      LTempVal := EmptyStr;
    end;
  end;
end;

我们现在需要一个函数来确定指定的文件是否与任何已定义的Extensions匹配:

function TFAutoSearch.MatchMask(const AFileName: String): Boolean;
var
  I: Integer;
  LExt: String;
begin
  Result := False;
  LExt := UpperCase(ExtractFileExt(LExt));
  for I := Low(FMask) to High(FMask) do
    if (LExt = FMask[I]) then
    begin
      Result := True;
      Break;
    end;
end;

现在这里是修改后的FileSearch程序:

procedure TFAutoSearch.FileSearch(const ARoot: String);
var
  LRoot: String;
  LRec: TSearchRec;
begin
  LRoot := IncludeTrailingPathDelimiter(ARoot);
  if FindFirst(LRoot + '*.*',LRec) = 0 then
  begin
    try
      repeat
        if (LRec.Attr and faDirectory <> 0) and (LRec.Name <> '.') and (LRec.Name <> '..') then
          FileSearch(LRoot + LRec.Name)
        else
        begin
          if (MatchMask(LRoot + LRec.Name)) then
            ListBox1.Items.Add(LRoot + LRec.Name);
        end;
      until (FindNext(LRec) <> 0);
    finally
      FindClose(LRec);
    end;
  end;
end;

最后,以下是您启动搜索的方式:

procedure TFAutoSearch.btnSearchClick(Sender: TObject);
begin
  ExplodeMask(edMask.Text);
  FileSearch(edPath.Text);
end;

其中edMask在您的问题中定义为Edit2,edPath在您的问题中定义为Edit1.请记住,此方法不支持使用Wildcard或其他特殊字符,因此edMask.Text应该类似.CBR; .CBZ

如果您使用Delphi的Regex库,您可以轻松修改此方法以支持您可以想象的所有表达式案例!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读