To start with, you can try this Applescript:
set interval to 7
set target to (current date) - interval
set oldCount to 0
tell application "Mail"
set box to mailbox named "INBOX" of account named "Your_mailbox_here"
set maxMessages to count of messages in box
repeat with index from 1 to maxMessages
set currentMessage to message index in box
set msgDate to date received of currentMessage
if msgDate is greater than target then
set oldCount to oldCount + 1
# do something here
Enter the above script in Script Editor, play with it till it does what you want, then save it as a script file (.scpt). run it with osascript(1).
Look at Applescript Resources and the Applescript Language Guide for more useful info. Also look at the scripting dictionary of Mail.app using the "Open Dictionary..." command in Script Editor to see what sort of things you can script in mail.