15
votes

I started a prototype with Delphi with all source files under one single directory.
Now, I want to reorganize everything with folders.

I found two way to do this.

1/ First is to open each unit in Delphi IDE and Save as the new folder. Then delete the old unit file.

2/ Under windows explorer, reorganize everything, then open the project in Delphi IDE, remove reference to old (unknown) units, and then add each unit folder by folder.

As I have 100+ units, it is really painful, whatever method I use (1 or 2).
How are you doing such reorganization ? Did I miss a feature ?

4
Fastest would be, I guess, to move your files and edit each .dpr manually when the IDE is closed. - Sertac Akyuz
I agree with @sertac - this is the sort of activity best done outside the IDE. Once you have reorganised your units drop into a command window and enter dir /s /b *.pas >units.txt from the root of your source directory. This will give you a file units.txt containing the new path to your unit. Use this to edit your DPR files. - Keith Miller
@KeithMiller don't forget that the .dproj files also need to be adjusted which can be a royal pain. @uweRaabe has a much better solution. - Jeroen Wiert Pluimers
Yes, - Uwe's solution is the easiest and best way. - Nick Hodges
@Jeroen - Looks like msbuild might need those entries, otherwise they're possibly redundant, AFAICT... In any case, there is not much editing involved, just delete 'dccreference' entries and then a 'save all' in the IDE regenerates them. - Sertac Akyuz

4 Answers

18
votes

I didn't check for XE, but in XE2 this is quite easy to do inside the IDE:

  • in case of multiple projects have all projects in one project group
  • in the project manager for each unit do
    • rename the unit by pressing F2 or use the context menu
    • while renaming place the subfolder in front of the unit name (e.g test\Unit1.pas)

The IDE will move the unit (removing the old one) and promote this change to all projects in the project group.

4
votes

Do you really need all 100+ units to be named in the project? Are not some of them simply common functions which could be placed in a folder on the search path? Delete all the less frequently needed units from the project and place them in folders to suit your logic. The IDE will find them, and you can also right-click open them in the IDE when you need to, without their being named in the project with a fixed path.

4
votes

The simplest way is:

  1. Copy project files to folders where you want them to be;
  2. Edit project *.dpr file to remove all in ..;
  3. Edit project options and add paths to project folders to search path option (not global IDE default project options, but your project's options).
2
votes

Only 2nd option. Move files to new directories using TotalCommander, Windows Explorer etc. I think your source layout should look like:

myutils
libs
    crypto
    database
    network

and then add those directories:

myutils;libs\crypto;libs\database;libs\network

to your Unit Search Path