A few weeks ago while working on a publically facing website built in MOSS 2007 I had to make some changes that affected all page layouts and master pages. The problem was overtime lots of test and unused page layouts and master pages had been left in the master page gallery. Rather than add the changes to all the page layouts and master pages I decided to write a tool that iterated through all webs and all pages in a site collection outputting the master page and page layout being used.
Since I am used to writing them I created a windows forms application which asks the user to supply the URL of the site, the only downside to this is the tool must be run on one of the web front end (WFE) servers.For my purposes I decided to take a snapshot of the site at a specific point and essentially export the data to a SQL DB. I create a variety of tables to store the information but I won’t cover this here as what I needed may not be required by most people. The reason I took this approach is I wanted to do some reporting on the data and didn’t want to be constantly programmatically accessing the site in case it had an effect on the performance.
Since I am not covering the DB side of things i have adjusted the code below to instead output all the details to a text file, this should give you an idea of what the process would be and you can alter it as you see fit.
Please note that while I have tested and ran this several times all use is at your own risk and strongly recommend this is tested in a development environment prior to being used on a production site.