0

我有很多 dfms。我正在使用此站点Delphi DFM properties remover中删除不需要的属性 exe来删除不再需要的已删除属性。以下代码可以正常工作,除了某些属性,例如如果我想删除带有名称的属性,Prop那么它会删除另一个带有名称的属性SecondProp以及Prop. mPropList 包含新行上的每个属性,例如 Prop1 Prop2 SecondProp Propn

我怀疑以下代码:

 if StrIPos(mPropList.Lines[K] + ' =', S) = 0 then
      begin
        LResult.Add(LSource[J]);
        continue;
      end;

SecondProp在这种情况下如何跳过?

我试过这个

if mPropList.Lines[K] = S then
      begin
        LResult.Add(LSource[J]);
        continue;
      end;

匹配精确的字符串,但它不起作用。

这是删除按钮代码

procedure TfrmDeleteProp_MainForm.Button2Click(Sender: TObject);
var
      LFile, LSource, LResult: TStringList;
      I, J, K, Processed, WasError, Removed: Integer;
      SkipPropData: Boolean;
      FLOpt: TFileListOptions;
      S: String;
begin
      LFile := TStringList.Create;
      LSource := TStringList.Create;
      LResult := TStringList.Create;
  try
    if chbxSubFolders.Checked then
      FLOpt := [flFullNames, flRecursive]
    else
      FLOpt := [flFullNames];

    if not AdvBuildFileList(IncludeTrailingBackslash(edPath.Text) + '*.DFM', faAnyFile, LFile, FLOpt) then
    begin
      MessageBox(Handle,
        'Invalid path specified. Can not process.',
        'Warning',
        MB_OK or MB_ICONEXCLAMATION);
      exit;
    end;

    Processed := 0;
    WasError := 0;

    for I := 0 to LFile.Count - 1 do
    begin
      LSource.LoadFromFile(LFile[I]);
      Removed := 0;

      for K := 0 to mPropList.Lines.Count - 1 do
      begin
        if K > 0 then
          LSource.Assign(LResult);

        if Trim(mPropList.Lines[K]) = '' then
          continue;

        LResult.Clear;
        SkipPropData := False;

        for J := 0 to LSource.Count - 1 do
        begin
          S := Trim(LSource[J]);

          if SkipPropData then
          begin
            if (S > '') and (S[Length(S)] in [')', '}']) then
              SkipPropData := False;
            continue;
          end;

          if StrIPos(mPropList.Lines[K] + ' =', S) = 0 then
          begin
            LResult.Add(LSource[J]);
            continue;
          end;

          if (S > '') and (S[Length(S)] in ['(', '{']) then
            SkipPropData := True;

          Removed := Removed + 1;
        end;
      end;

      if Removed > 0 then
      begin
        if RenameFile(LFile[I], ChangeFileExt(LFile[I], '.BAK')) then
        begin
          LResult.SaveToFile(LFile[I]);
          Processed := Processed + 1;
        end else
        begin
          MessageBox(Handle,
            PChar('Can not create back up copy for file: ' + LFile[I] + #13#10'File is not processed.'),
            'Warning',
            MB_OK or MB_ICONEXCLAMATION);
          WasError := WasError + 1;
        end;
      end;
    end;

    MessageBox(Handle,
      PChar(Format('Total files found: %d'#13#10'Properties were removed from: %d'#13#10'Error files: %d',
        [LFile.Count, Processed, WasError])),
      'Statistics',
      MB_OK or MB_ICONINFORMATION);
  finally
    LFile.Free;
    LSource.Free;
    LResult.Free;
  end;
end;
4

1 回答 1

1

我们确定:

  • 您的mPropList.Lines每个都包含类似Prop =[...]orSecondProp =[...]的内容,并且属性名称始终以该行开头。
  • LResult保存应该保留的行。所以,如果字符串匹配,你不想添加它,但如果它们匹配,你就这样做。

将这些放在一起,您只想LResult.Add()StrIPos(...) <> 1. 这样,您添加

  • 不包含属性名称 ( StrIPos() == 0, ie < 1) 的行和
  • 包含它作为子字符串但不在开头的行,即仅在另一个属性名称中 ( StrIPos > 1)。

因此,固定代码将如下所示:

if StrIPos(mPropList.Lines[K] + ' =', S) <> 1 then
  begin
    LResult.Add(LSource[J]);
    continue;
  end;
于 2020-06-22T14:48:56.520 回答