Search code examples
delphidelphi-7

How to set text dfm value to checked on all dfm files/Count number of lines in .dfm binary file


with below example i am counting number of lines in .dfm file and the count is coming wrong because .dfm is saved in binary format.

if i open .dfm file and do right click and set text dfm to checked and the count is coming correctly. Below is the code

function TForm1.FindNumberOfLinesInFile(FileName: String): Integer;
var
  contents : TStringList;
  filestream : TFileStream;
  outStream : TMemoryStream;
begin
  try
    try
      Result := 0;
      contents := TStringList.Create;
      if edtFileToSearch.Text = '.dfm' then
      begin
        contents.LoadFromFile(FileName);
        //i am binary
        if pos('OBJECT', Uppercase(contents[0])) = 0 then // Count is coming wrong with this
        begin
          contents.Clear;
          fileStream := TFileStream.Create(FileName, fmShareDenyNone);
          outStream := TMemoryStream.Create;
          try
            ObjectResourceToText(filestream,outStream);
            outStream.Position := 0;
            Contents.LoadFromStream(outStream);
          finally
            FreeAndNil(outStream);
          end;
        end
        else
        begin
          fileStream := TFileStream.Create(FileName, fmShareDenyNone);
          Contents.LoadFromStream(fileStream);
        end;
      end
      else
      begin
          fileStream := TFileStream.Create(FileName, fmShareDenyNone);
          Contents.LoadFromStream(filestream);
      end;
      Result := contents.Count;
    finally
      FreeAndNil(fileStream);
      FreeAndNil(contents);
    end;
  except
    on e: Exception do Result := -1;
  end;
end;

i have 2 questions

1)how to set text dfm value to checked in all dfm files(i have around 1000 dfm files)?

2)how load binary file correctly and count number of lines?


Solution

  • Delphi comes with a command line tool to do this, named convert. Open up a command prompt and ensure that your Delphi bin directory is in the PATH. Then type:

    C:\projects\myprocject> convert
    

    The output will be something like this:

    Delphi Form Conversion Utility Version 5.0
    Copyright (c) 1995,99 Inprise Corporation
    Usage: convert.exe [-i] [-s] [-t | -b] 
      -i  Convert files in-place (output overwrites input)
      -s  Recurse subdirectories
      -t  Convert to text
      -b  Convert to binary
    

    So, you should be able to write:

    C:\projects\myprocject> convert -i -s -t *.dfm
    

    to effect the change required.