程序启动后扫描文件

问题描述

我在Lazarus工具中添加一个TToggleBox,现在我希望根据位于同一目录中文件中字符串的存在来对其进行设置。因此,我希望Lazarus在程序启动后立即检查字符串文件。如果它包含字符串(例如“ hello world”),则应将ToggleBox1的标题(请参见下面)设置为domain.com/recipes/fruits/apples/ ...,并且如果字符串不存在于domain.com/_custom_post_type_/_taxonomy_/_term_/ ...。我该怎么做?

TToggleBox

"Activated"

此外,在通过工具设置了标题切换框之后,我想进一步与之交互。意思是,当未找到字符串并且ToggleBox标题已设置为"Deactivated"时,我要按ToggleBox启动cmd脚本,并将Caption设置为procedure TForm1.ToggleBox1Change(Sender: TObject); begin if ToggleBox1.Checked then begin ToggleBox1.Caption:='Deactivated' end else ToggleBox1.Caption:='Activated'; end; 并反转(如果找到字符串并通过按ToggleBox标题设置为"Deactivated",我想将标题设置为"Activated"并启动另一个cmd脚本)。你该怎么做?

解决方法

您可以使用TStringList从磁盘加载文件(假定它是文本文件),然后使用Pos()函数查看字符串列表的Text属性是否包含感兴趣的字符串。例如:

function TextFileContains(cost AFileName,AString : String) : boolean;
var
  StringList : TStringList;
begin
  StringList := TStringList.Create;
  try
    Stringlist.LoadFromFile(AFileName);  //  Note:  AFileName should include the full path to the file
    Result := Pos(AString,StringList.Text) > 0;
  finally
    StringList.Free;
  end;
end;

我假设您可以弄清楚如何在代码中使用此功能以获得所需的结果。如果没有,请发表评论。