To get a listing of all files in the current directory see
this thread, actually its short enough that I might as well post the code again.
Now, rather than printing out the filename, you can use the
string function (stringname).endsWith(".txt") to see if it has the .txt extention. That will give you all .txt files in the current directory. To do sub directories, you will need to loop through and (recursively) check each subsequent directory for txt files as well as more directorys.
If you need help with recusrion, check out a google search
here.
If you need more help, just post.