We can't help you much when you post irrelevant code... but Corion's already mentioned that.
But since your narrative remark sounds as though you're merely RENAMING files (ie, merely changing the Win-style extension or "file type" from .txt to .dat), you don't need to read the files and re-write them; just rename.
Yet another gimmé request! Sorry, we expect SOPW to seek wisdom, not to ask us to do so for them.
Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:- relevant (real) code
- verbatim error and/or warning messages
- a coherent explanation of what "doesn't work actually means.