【问题标题】:FindFirst cannot find all files with extension ~1~ and othersFindFirst 找不到所有扩展名为 ~1~ 的文件和其他文件
【发布时间】:2020-08-06 10:17:57
【问题描述】:

SysUtils.FindFirst找不到所有扩展名为~1~的文件以及其他奇怪的文件扩展名,例如:Unit1.dfm.~1~

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TSearchRec sr;
  int iAttributes = 0;
  StringGrid1->RowCount = 1;
  iAttributes |= faReadOnly;
  iAttributes |= faHidden;
  iAttributes |= faSysFile;
  iAttributes |= faVolumeID;
  iAttributes |= faDirectory;
  iAttributes |= faArchive;
  iAttributes |= faAnyFile;
  StringGrid1->RowCount = 0;
  if (FindFirst(Edit1->Text, iAttributes, sr) == 0)
  {
    do
    {
      if ((sr.Attr & iAttributes) == sr.Attr)
      {
        StringGrid1->RowCount = StringGrid1->RowCount + 1;
        StringGrid1->Cells[1][StringGrid1->RowCount-1] = sr.Name;
        StringGrid1->Cells[2][StringGrid1->RowCount-1] = IntToStr(sr.Size);
      }
    } while (FindNext(sr) == 0);
    FindClose(sr);
  }
}

如何让这段代码找到所有文件,任何扩展名?

【问题讨论】:

  • 您是否尝试过删除代码中的属性检查?我觉得这很奇怪。
  • 您可能还想重命名您的复选框和编辑框。
  • 代码直接来自 FindFirst 帮助的某些版本中的示例。前任。 docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/…
  • @Brian,这不符合要求。菲如果设置了 faArchive,您将只获取具有归档属性的文件,而不会获取没有归档属性的文件。如果 faArchive 没有设置,你会得到所有没有存档属性的文件,但不是那些有它的文件。如果您想检查文件是否具有任何选定的属性集,您最好检查 != 0 而不是 == sr.Attr
  • 显示传递给函数的内容 - 或更改代码以直接使用这些值以获得最小、可重现的示例。

标签: delphi c++builder rad-studio


【解决方案1】:

以下代码将显示目录中的所有项目,包括 .、..、.~* 文件 - 例如在 C++Builder 项目的历史文件夹中:D

我在编辑框中输入了以下字符串:

C:\Users\david\Documents\Embarcadero\Studio\Projects\FindFirstVCLCpp\__history\*.*

执行后的 TMemo 包含以下内容:

. = 0
.. = 0
Unit1.cpp.~1~ = 991
Unit1.cpp.~2~ = 996

按钮处理程序的源代码:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TSearchRec sr;
  Memo1->Lines->Clear();
  int iAttributes = 0;
  iAttributes |= faReadOnly;
  iAttributes |= faHidden;
  iAttributes |= faSysFile;
  iAttributes |= faVolumeID;
  iAttributes |= faDirectory;
  iAttributes |= faArchive;
  iAttributes |= faAnyFile;
  if (FindFirst(Edit1->Text, iAttributes, sr) == 0)
  {
    do
    {
      if ((sr.Attr & iAttributes) == sr.Attr)
      {
        Memo1->Lines->Add(
          sr.Name
          + " = "
          + IntToStr(sr.Size)
        );
      }
    } while (FindNext(sr) == 0);
    FindClose(sr);
  }
}

【讨论】:

    猜你喜欢
    • 2021-02-17
    • 2012-11-13
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多