A More Robust Line Counter

Posted On Thu, 23 May 2013

Filed under Batch
Tags: , , , , ,

Comments Dropped 2 responses

In a previous post, I talked about how to count the number of lines in a text file. I explained the technique of piping the output from type file.txt into find /c /v "" and wrapping the whole thing inside a for /f loop to store the result in a variable. A simple and effective solution to a common Batch programming task. 🙂

Too bad it doesn’t work…

continue reading »

Advertisements

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 »

Copy a Long Line from a Text File and Save It to a New File

This is a follow-up to my recent post on how to store the nth line of a text file in a variable. The solution given won’t work for extremely long lines because the line requested by the user is stored in a variable and variables in Batch can only hold up to 8191 characters.

If you need to select an extremely long line from a file and save it to a new file, there is a workaround. But it ain’t pretty, or efficient. It involves more, findstr, and a whole lot of temporary files. 😈

continue reading »

Store Nth Line of a Text File in a Variable

Posted On Sat, 10 Nov 2012

Filed under Batch
Tags: , , ,

Comments Dropped leave a response

This question crops up from time to time: “How do I save a line from a file into a variable?” It seems like a perfectly reasonable question to ask. You might think the answer is equally as straightforward. Until you try to write a Batch program to do it, that is…

Read the rest of this post »

Convert Newlines from Windows to Unix

Posted On Wed, 24 Oct 2012

Filed under Batch
Tags: , , , ,

Comments Dropped leave a response

16-Apr-2013: Updated source code. Rewrite of post.

Everybody knows the old trick of converting a text file with Unix newlines (LF) to Windows line-endings (CR+LF):

more unix.txt > win.txt

(Note that more will wait for a keypress after scrolling 65,534 lines, even if output is redirected to a pipe or file.) But converting from Windows to Unix is a far more complicated affair. After searching failed to find any straightforward Batch solutions—apart from this meandering thread on DosTips—I cranked out win2unix.cmd as outlined 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 »

Extract a Range of Lines from a Text File

Posted On Mon, 27 Aug 2012

Filed under Batch
Tags: , , ,

Comments Dropped leave a response

Below is a little Batch program to extract a range of lines from a text file and output them to the screen. A simple enough task, or so I thought!

Read the rest of this post »

Count the Number of Lines in a Text File

Posted On Thu, 9 Aug 2012

Filed under Batch
Tags: , ,

Comments Dropped leave a response

I have of late, wherefore I know not, started writing little Batch programs. Batch is ugly and buggy, but at the same time its raw minimalism makes it capable of performing surprisingly complicated tasks. Ah, but knowing how to exploit this power, therein lies the rub!

Let's start with the seemingly straightforward task of counting the lines in a text file and storing the result in a variable.

Read the rest of this post »