@cms_phil – this is only good for the ordinary InDesign CS4 or CS3 file. In CS5 and above you could mix "landscape" and "portrait" oriented pages in one single InDesign document.
For CS5 and above you have to loop through all pages and check width and height to calculate the orientation. I'm not sure that is enough, because you can do a lot of different transformations to a specific page that will affect the orientation of output (e.g. exported PDF files).
See what scripting can also do with CS4 files (and their spreads) regarding transformation:
Europe, Middle East and Africa