Scan Text Files for Control Characters

Posted On Tue, 16 Apr 2013

Filed under Batch
Tags: , , ,

Comments Dropped leave a response

A large part of programming in Batch is taken up with processing text files. By “text file”, I mean a plain text file with Windows line endings. And “plain text” means no nasty control characters such as Control-Z or the infamous Null Character.

For example, the former is used by copy /a and type as the end-of-file marker, while set and echo interpret the latter as the end of input.

So it’s always a good idea to scan any text files of unknown origin for these troublesome characters before doing anything else. Which is why I wrote the ctrlscan.cmd program described below…

continue reading »

Ensure Text Files End with a Newline

Posted On Thu, 4 Oct 2012

Filed under Batch
Tags: , , , ,

Comments Dropped leave a response

From time to time, you may find yourself building up a file comprised of command output and snippets from other files. On these occasions, you will no doubt be using pipes and redirection, and commands such as copy, findstr, and type.

Which is all well and good except that you could easily run into trouble if some of the bits and pieces you're cobbling together end with a newline while others don't. For instance, type will join the last line of a file that doesn't end with a newline with the first line from the next file, forming one long line. Probably not what you want.

And according to Dave Benham in his treatise on the Undocumented Features and Limitations of FindStr, findstr under XP and Win7 will hang if redirected input doesn't end with a linefeed character.

Read the rest of this post »