In a nutshell – remove the newline at the end of the previous line because that is the source of the empty line at the bottom of the file.
Story time:
(I give up on trying to get my pictures straight)
I recently created an ETL process to pull data from a SQL Server and export it to a flat file. The issue came when the file was being consumed by a mainframe. The mainframe process did not like the default last blank/empty line at the end of the file. (Actually, it’s not considered a line. Look here.)
So I sat back and thought how I would get rid of that last line, until I finally remembered it was because the prior line has a “newline” (\r\n) character at the end of it. This is what resulted in the blank/empty line at the end.
Ah-Hah! Simple solution huh? Took me way longer than it should’ve, but that’s the joy of programming. This was also a reminder to myself that one should look at things from all angles when approaching a problem. Anyway, this was my initial quick-n-dirty fix in a script task in a SSIS package, but you can easily implement it in a console app: