Q:  How can I change the form as text?  (DFM -> TXT -> DFM)

A:  Here is something that was sent to me.  I tried it and it worked well for me.  Enjoy!

Delphi Form to Text file converter.
----------------------------------

What is it?
-----------

This is a program to convert DFM files (Delphi form files) to their textual representation and back again.

How does it work?
-----------------

Take a look at the conversion function. Its _really_ easy.

Why do I need this program?
---------------------------

Recently, I took the decision to write a new component called MButton that allowed the caption and hint of a button to be loaded from a resource only DLL (to provide multi-lingual support). It was a major job to go
into each form, using Delphi, and convert them all.

I ended up going into each form, saving it as text, using FileFind to do search and replace and then converting back to a form. This still took a LONG time. With this program the whole job could be done in about 5 minutes.

How this came about
-------------------

While searching through some old CIS messages I came across a message in which someone described how to convert a DFM file to a TXT file. I tried it and it worked! I've added a front-end to make it easier to convert multiple files and allow file selections.

If you're the person who posted the original message. Thanks.

What do I want from you?
-----------------------

This program is free. Just send me an e-mail message if you like it, have any suggestions for improvements etc.

The usual warnings
------------------

THIS PROGRAM IS PROVIDED AT YOUR OWN RISK.

You MUST make sure that you have backed up all your forms before converting them. This is common sense. The author cannot, and will not, be held responsible in anyway for any problems caused by use or misuse of
this program. Having said all that: the program uses exactly the same routines as the Delphi IDE for conversions. It SHOULD be safe!

John Wright (CIS 100335,322)

unit Ufile;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, FileCtrl, ExtCtrls;

type

  TDFMOrTXT = (ConvertToForm, ConvertToText) ;

  TForm1 = class(TForm)
    ResultLabel: TLabel;
    StatusLabel: TLabel;
    Wizard: TNotebook;
    RadioGroup2: TRadioGroup;
    GroupBox1: TGroupBox;
    FileListBox1: TFileListBox;
    DirectoryListBox1: TDirectoryListBox;
    DriveComboBox1: TDriveComboBox;
    FilterComboBox1: TFilterComboBox;
    Panel1: TPanel;
    pbPrevious: TButton;
    pbNext: TButton;
    pbConvert: TButton;
    pbExit: TButton;
    RadioGroup1: TRadioGroup;
    Step3lLabel: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    pbAboutMe: TButton;
    procedure pbConvertClick(Sender: TObject);
    procedure pbExitClick(Sender: TObject);
    procedure RadioGroup2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    Function ConvertFormOrText(FileToConvertFrom : string) : boolean ;
    procedure ProcessAllFiles(const Extension : string) ;
    procedure pbNextClick(Sender: TObject);
    procedure pbPreviousClick(Sender: TObject);
    procedure pbAboutMeClick(Sender: TObject);

  private

    { This holds the current type of conversion we are performing }

    ConversionType : TDFMOrTXT ;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Function TForm1.ConvertFormOrText(FileToConvertFrom : string) : boolean ;
Var
  InputStream, OutputStream : TFileStream;
  FileToConvertTo : string ;
Begin

  { 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 }
 

  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 ;
      On EGPFault do result := False ;
    End ;
  Finally

    InputStream.Free;
    OutputStream.Free;
  End ;
End;

procedure TForm1.ProcessAllFiles(const Extension : string) ;
var
  SourceFile : string ;
  d : string ;
  SearchRec: TSearchRec;
  RetCode : integer ;
  FilesConverted : Integer ;
Begin

  try

    Screen.cursor := crHourGlass ;

    { Routine to process all files of a specified type }

    FilesConverted := 0 ;

    d := DirectoryListBox1.directory+'\' ;

    RetCode := FindFirst(d+Extension, 0, SearchRec);
    while RetCode = 0 do
    Begin
      SourceFile := d+SearchRec.Name ;
      if ConvertFormOrText(SourceFile) then
        Inc(FilesConverted) ;

      RetCode := FindNext(SearchRec) ;
    end ;

    ResultLabel.Caption := inttostr(FilesConverted)+' file(s) converted correctly' ;
  Finally
    Screen.Cursor := crDefault ;
  End ;

End ;

procedure TForm1.pbConvertClick(Sender: TObject);
var
  SourceFile : string ;
begin

  { Choose whether to convert the selected file or
    all the files }

  case RadioGroup1.ItemIndex of
    0 :
    Begin
      SourceFile := FileListBox1.FileName ;
      if fileexists(SourceFile) then
      Begin
        if ConvertFormOrText(SourceFile) then
          ResultLabel.Caption := 'Done'
        else
          ResultLabel.caption := 'Failed to convert - may be invalid format'
      End
      else
        ResultLabel.Caption := 'File Not Found' ;
    end ;
    1 :
    Begin

      { Process all the files in the selected directory }

      case ConversionType of
        ConvertToText : ProcessAllFiles('*.DFM') ;
        ConvertToForm : ProcessAllFiles('*.TXT') ;
      End ;
    End ;
  end ;
end;

procedure TForm1.pbExitClick(Sender: TObject);
begin
  close ;
end;

procedure TForm1.RadioGroup2Click(Sender: TObject);
begin

  { Swap TXT to DFM or DFM to TXT }

  case RadioGroup2.itemIndex of
   0 :
    Begin
      pbConvert.caption := 'DFM -> TXT' ;
      FileListBox1.Mask := '*.DFM' ;
      FilterComboBox1.Filter := 'Form Files|*.DFM' ;
      ConversionType := ConvertToText ;
    End ;
    1 :
    Begin
      pbConvert.caption := 'TXT -> DFM' ;
      FileListBox1.Mask := '*.TXT' ;
      FilterComboBox1.Filter := 'Text Files|*.TXT' ;
      ConversionType := ConvertToForm ;
    End ;
  end ;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  CurrentDirectory : string ;
begin
  ConversionType := ConvertToText ;
  pbConvert.caption := 'DFM -> TXT' ;
  FileListBox1.Mask := '*.DFM' ;

  GetDir(0, CurrentDirectory) ;
  DirectoryListBox1.directory := CurrentDirectory ;

end;

procedure TForm1.pbNextClick(Sender: TObject);
begin

  { Next screen button }

  pbConvert.Enabled := false ;
  pbNext.Enabled := True ;
  pbPrevious.Enabled := True ;
  case Wizard.PageIndex of
    0 :
    Begin
      Wizard.PageIndex := Wizard.PageIndex + 1 ;
    End ;
    1 :
    Begin
      Wizard.PageIndex := Wizard.PageIndex + 1 ;
      pbConvert.Enabled := True ;
      pbNext.enabled := false ;

    End ;
  end ;
end;

procedure TForm1.pbPreviousClick(Sender: TObject);
begin

  { Previous Screen button }

  pbConvert.Enabled := false ;
  pbNext.Enabled := True ;
  pbPrevious.Enabled := True ;
  case Wizard.PageIndex of
    1 :
    Begin
      Wizard.PageIndex := Wizard.PageIndex - 1 ;
      pbPrevious.enabled := false ;
    End ;
    2 :
    Begin
      Wizard.PageIndex := Wizard.PageIndex - 1 ;
      ResultLabel.caption := '' ;
    End ;
  end ;

end;

procedure TForm1.pbAboutMeClick(Sender: TObject);
begin
  MessageDlg('Written by John Wright (CIS 100335,322) (c) 1995.'+#13+'Use at your own risk.', mtInformation, [mbOK], 0) ;
end;

end.

{*******DPR*******}

program Dfm2txt;

uses
  Forms,
  Ufile in 'UFILE.PAS' {Form1};

{$R *.RES}

begin
  Application.Title := 'Delphi Form to Text file converter';
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

{*******DFM*******}

object Form1: TForm1
  Left = 207
  Top = 102
  ActiveControl = pbNext
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsDialog
  Caption = 'Delphi Form Conversion Utility'
  ClientHeight = 370
  ClientWidth = 535
  Font.Color = clBlack
  Font.Height = -13
  Font.Name = 'System'
  Font.Style = [fsBold]
  PixelsPerInch = 96
  Position = poScreenCenter
  OnCreate = FormCreate
  TextHeight = 16
  object ResultLabel: TLabel
    Left = 72
    Top = 12
    Width = 4
    Height = 18
    Font.Color = clBlack
    Font.Height = -16
    Font.Name = 'Arial'
    Font.Style = [fsBold, fsItalic]
    ParentFont = False
  end
  object StatusLabel: TLabel
    Left = 12
    Top = 12
    Width = 49
    Height = 19
    Caption = 'Status'
    Font.Color = clBlack
    Font.Height = -16
    Font.Name = 'Arial'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Wizard: TNotebook
    Left = 0
    Top = 44
    Width = 535
    Height = 269
    Align = alBottom
    TabOrder = 0
    object TPage
      Left = 0
      Top = 0
      Caption = 'File Types'
      object Label4: TLabel
        Left = 16
        Top = 20
        Width = 377
        Height = 17
        Caption = 'Step 1 : Select the type of conversion you wish to perform.'
        Font.Color = clBlack
        Font.Height = -15
        Font.Name = 'Arial'
        Font.Style = []
        ParentFont = False
      end
      object Label5: TLabel
        Left = 16
        Top = 40
        Width = 43
        Height = 18
        Caption = 'Either'
        Font.Color = clBlack
        Font.Height = -15
        Font.Name = 'Arial'
        Font.Style = [fsBold]
        ParentFont = False
      end
      object Label1: TLabel
        Left = 16
        Top = 84
        Width = 15
        Height = 18
        Caption = 'or'
        Font.Color = clBlack
        Font.Height = -15
        Font.Name = 'Arial'
        Font.Style = [fsBold]
        ParentFont = False
      end
      object Label2: TLabel
        Left = 16
        Top = 108
        Width = 221
        Height = 17
        Caption = '2. Convert a TXT file to a DFM file.'
        Font.Color = clBlack
        Font.Height = -15
        Font.Name = 'Arial'
        Font.Style = []
        ParentFont = False
      end
      object Label6: TLabel
        Left = 16
        Top = 64
        Width = 221
        Height = 17
        Caption = '1. Convert a DFM file to a TXT file '
        Font.Color = clBlack
        Font.Height = -15
        Font.Name = 'Arial'
        Font.Style = []
        ParentFont = False
      end
      object RadioGroup2: TRadioGroup
        Left = 16
        Top = 150
        Width = 185
        Height = 89
        Caption = 'Type of Conversion'
        ItemIndex = 0
        Items.Strings = (
          'Convert To Text'
          'Convert To Form')
        TabOrder = 0
        OnClick = RadioGroup2Click
      end
    end
    object TPage
      Left = 0
      Top = 0
      Caption = 'Number of Files'
      object Label3: TLabel
        Left = 16
        Top = 20
        Width = 368
        Height = 17
        Caption = 'Step 2 : Choose how many files do you want to convert ?'
        Font.Color = clBlack
        Font.Height = -15
        Font.Name = 'Arial'
        Font.Style = []
        ParentFont = False
      end
      object Label7: TLabel
        Left = 16
        Top = 44
        Width = 421
        Height = 17
        Caption = 'Selecting all files will convert all the files in the specified directory.'
        Font.Color = clBlack
        Font.Height = -15
        Font.Name = 'Arial'
        Font.Style = []
        ParentFont = False
      end
      object RadioGroup1: TRadioGroup
        Left = 16
        Top = 150
        Width = 169
        Height = 89
        Caption = 'How many ?'
        ItemIndex = 0
        Items.Strings = (
          'One file'
          'All Files')
        TabOrder = 0
      end
    end
    object TPage
      Left = 0
      Top = 0
      Caption = 'Files'
      object Step3lLabel: TLabel
        Left = 16
        Top = 20
        Width = 288
        Height = 17
        Caption = 'Step 3 : Choose the files you wish to convert'
        Font.Color = clBlack
        Font.Height = -15
        Font.Name = 'Arial'
        Font.Style = []
        ParentFont = False
      end
      object GroupBox1: TGroupBox
        Left = 8
        Top = 44
        Width = 433
        Height = 217
        Caption = 'Choose a file to convert'
        TabOrder = 0
        object FileListBox1: TFileListBox
          Left = 16
          Top = 24
          Width = 141
          Height = 146
          IntegralHeight = True
          ItemHeight = 16
          Mask = '*.DFM'
          TabOrder = 0
        end
        object DirectoryListBox1: TDirectoryListBox
          Left = 172
          Top = 24
          Width = 241
          Height = 146
          Hint = 'Directory where all selected and converted files are stored'
          FileList = FileListBox1
          IntegralHeight = True
          ItemHeight = 16
          ParentShowHint = False
          ShowHint = True
          TabOrder = 1
        end
        object DriveComboBox1: TDriveComboBox
          Left = 172
          Top = 177
          Width = 241
          Height = 22
          DirList = DirectoryListBox1
          TabOrder = 3
        end
        object FilterComboBox1: TFilterComboBox
          Left = 16
          Top = 177
          Width = 141
          Height = 24
          FileList = FileListBox1
          Filter = 'All file (*.dfm)|*.DFM'
          TabOrder = 2
        end
      end
    end
  end
  object Panel1: TPanel
    Left = 0
    Top = 313
    Width = 535
    Height = 57
    Align = alBottom
    TabOrder = 1
    object pbPrevious: TButton
      Left = 16
      Top = 12
      Width = 89
      Height = 33
      Hint = 'Go to the previous screen'
      Caption = '<<'
      Enabled = False
      ParentShowHint = False
      ShowHint = True
      TabOrder = 0
      OnClick = pbPreviousClick
    end
    object pbNext: TButton
      Left = 112
      Top = 12
      Width = 89
      Height = 33
      Hint = 'Go to the next screen'
      Caption = '>>'
      ParentShowHint = False
      ShowHint = True
      TabOrder = 1
      OnClick = pbNextClick
    end
    object pbConvert: TButton
      Left = 208
      Top = 12
      Width = 89
      Height = 33
      Hint = 'Convert the file'
      Caption = 'DFM -> TXT'
      Enabled = False
      ParentShowHint = False
      ShowHint = True
      TabOrder = 2
      OnClick = pbConvertClick
    end
    object pbExit: TButton
      Left = 304
      Top = 12
      Width = 89
      Height = 33
      Hint = 'Exit the program'
      Cancel = True
      Caption = '&Exit'
      ModalResult = 2
      ParentShowHint = False
      ShowHint = True
      TabOrder = 3
      OnClick = pbExitClick
    end
    object pbAboutMe: TButton
      Left = 436
      Top = 12
      Width = 89
      Height = 33
      Hint = 'About the Author'
      Caption = '&Author'
      ParentShowHint = False
      ShowHint = True
      TabOrder = 4
      OnClick = pbAboutMeClick
    end
  end
end