This site is temporarily down. Please come back. Convert between a DFM and a TXT file
DelphiFAQ Home Search:

Convert between a DFM and a TXT file

 

comments1 comments. Current rating: 4 stars (1 votes). Leave comments and/ or rate it.

A conversion from DFM (binary Delphi Form file) to a text file is done easily using the function ObjectResourceToText. For the opposite direction, use ObjectTextToResource.

The following code sample packs the whole task in a handy function.
Usage:

// convert a DFM file to TXT
ConvertFormOrText('e:\temp\unit11.dfm', ConvertToText);

// vice versa, extension is not necessary
ConvertFormOrText('e:\temp\unit11', ConvertToDFM);

type
  TDFMorTXT = (ConvertToForm, ConvertToText);

{ Given a file name this routine will convert the file from either
  1. A text file to a DFM file or
  2. A DFM file to a text file
  The output file name is built from the input file name }
function ConvertFormOrText(FileToConvertFrom : string;
                           ConversionType : TDFMorTXT) : boolean;
var
  InputStream, OutputStream : TFileStream;
  FileToConvertTo : string;
begin
  Result := True;
  FileToConvertTo := FileToConvertFrom;

  { change file extensions as appropriate }
  case ConversionType of
    ConvertToForm:
    begin
      FileToConvertFrom := ChangeFileext(FileToConvertFrom, '.TXT');
      FileToConvertTo := ChangeFileext(FileToConvertFrom, '.DFM');
    end;
    ConvertToText:
    begin
      FileToConvertFrom := ChangeFileext(FileToConvertFrom, '.DFM');
      FileToConvertTo := ChangeFileext(FileToConvertFrom, '.TXT');
    end;
  end;

  try
    try
      { Create a file stream for the specified file }
      InputStream  := TFileStream.Create(FileToConvertFrom, fmOpenRead);
      OutputStream := TFileStream.Create(FileToConvertTo, fmCreate);

      { Now perform the selected conversion }
      case ConversionType of
        ConvertToForm : ObjectTextToResource(InputStream, OutputStream);
        ConvertToText : ObjectResourceToText(InputStream, OutputStream);
      end;
    except
      On EStreamError do Result := False;
    end;
  finally
    InputStream.Free;
    OutputStream.Free;
  end;
end;

Content-type: text/html

Comments:

2008-03-14, 13:00:13
[hidden] from South Africa  
rating
Great, I could use this, but any idea whether running Form to Text on a file that is already text wil mess the file up or not? If it would, how could I detect programtically that it has not been converted yet?

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: