Delphi .NET (2) Database (71) Delphi IDE (89) Network (39) Printing (3) Strings (12) VCL (83) Windows with Delphi (280)
Exchange Links About this site Links to us 
|
Convert between a DFM and a TXT file
1 comments. Current rating: (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;
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
InputStream := TFileStream.Create(FileToConvertFrom, fmOpenRead);
OutputStream := TFileStream.Create(FileToConvertTo, fmCreate);
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;
| |  | |  |
Comments:
|
[hidden] from South Africa
|
 |
|
|
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?
|
|