Bulk-moving units in Delphi

If you change the structure of a Delphi project, you will often face the challenge of moving many units from one directory to another, e.g. you change the name of a subdirectory from src to source:

myproject
  \myproject.dpr
  \myproject.dproj
  \src  <=== change to "source"
    \MyUnit1.pas
    \MyForm1.pas
    \MyForm1.dfm

In ancient Delphi versions, you would just open the .dpr file in a text editor, use search and replace to change the path and the Delphi IDE would happily work with that new .dpr file.

Starting from Delphi 2007 the IDE stores the path to the units included in the project in two files: The .dpr file and the .dproj file. If they don't match, you can get all sorts of weird errors even though it seems to work at first.

That leaves you the following options:

  • You could open your project in the Delphi IDE remove all units from it and add them again at the new location. No problem if there are only a hand full of units, but with a typical large project this could easily take half an hour and is rather boring (and therefore error prone).
  • You could change the paths not just in the .dpr file but also in the .dproj file using search and replace. But beware, the text you replace might be used in some other places too which you might not want to change. XML files are not really meant to be modified by humans.
  • You could change the paths in the .dpr file and simply delete all
    <DCCReference Include="path goes here">
      <Form>form name goes here</Form>
    </DCCReference>

    entries from the .dproj file. This is much easier done than a search and replace. Afterwards you open it in the IDE, make a small change to the .dpr file (e.g. add and delete a white space) and save it. The IDE will happily recreate all these entries for you.