In writing Dynamics integrations, I'm frequently asked to read a directory of files and process them somehow. Sometimes they're XML, sometimes text.
This piece of code is the main loop for that task. It's not ground breaking or brilliant; but it saves me 30-60 minutes of coding to have it ready so that I can copy and paste into an app.
The basic logic is this:
Create 'done' and 'error' folders to move the files into after processing
Loop
Process the file
Move the file to 'done'
Trap errors
Move the file to 'error
Handle the error