Another option could be this: Build two separate files, one that has the messages, and another that keeps the counter. When you are ready to send the complete file, concatenate the two together putting the number of messages at the top. Just a thought.