Merging Similar Materials

Can I take all the materials in my scene and remove similar materials, combining them into one material?

I’ve got a piece of converted geometry. In it are over 100 parts, each with their own material. It looks something like this.

part
part.001
part.002

part.099

and each part has a unique material set, for instance

material
material.001
material.002

material.099

I would like to make it so all these part share the same material. Is there a way to compare materials automatically and combine them. I know how to do it by hand (select part, select appropriate material index, select the datablock for the material) but this is laborious for so many parts.

Is there a way already?

If not, is this something that could be scripted? (Easy or hard?)

Many thanks,
ryan

Not sure if it is exactly what you want but you can copy a material from one object to mulitple objects.
Select all objects you want to have the material making sure that the last object selected has the materials you want to copy. Then Ctl+L (Make Links) / Materials.
Save you blend. Reopen and any unused materials will have been deleted.

Richard