Originally published on: Sun, 06 Sep 2009 16:12:42 +0000
Please note! If you're having difficulties compiling the C source code presented below, please see my post: Compiling C from the Command Line with Pelles C
Although a number of more robust scripting options are available, I still write batch files in the Windows environment. I enjoy the simplicity but sometimes find myself frustrated with something that I perceive to be a limitation.
Usually, if I hit a wall trying to perform something that can be a cumbersome in a batch file, I will often just write a separate program or script to perform the task.
In some cases, when I need to construct an output text file, I find myself opting for Perl or AWK or something similar if I need to embed special-characters in the output. The greater-than '>' and less-than '<' symbols can be a bit troublesome in a batch file because they handle redirection. This makes building an XML file a little tricky. The & symbol can sometimes cause problems too as it is supposed to be used to chain commands together.
As a solution to my own problem, I wrote this simple version of the batch echo command called echoj. While the EXE provided in a link later in this text is intended for use in a Windows environment, the C code should be portable to other environments.
Here's the C source:
echoj.c
If you run echoj from a command-prompt with no arguments, you should see the following help screen:
Let's examine the different options and different ways to use echoj.
The -v option is intended to provide a simple way to verify that echoj.exe is in your command processor's execution path. It accepts a number as an argument and terminates the echoj process immediately, returning that number.
In a batch file, you might leverage feature in this manner.
Without any parameters, echoj can be useful when needing to write special characters to the console. Consider the following example.
The $09 code is the ASCII code for the TAB character. echoj will translate any string on the command line ( after the optional parameters ) from its hexadecimal representation into binary if:
Note that echoj doesn't work like the normal echo. If you enter:
You'll likely see:
echoj looks for parameters separated by spaces. If you need to include spaces in a parameter, enclose that parameter in double-quotes.
So, the TAB example would also work the same way if we wrote:
...and would also work if we quote the hexadecimal paramters.
You can display the greater-than, less-than, ampersand, and pipe characters by using $3c, $3e, $26, and $7c respectively.
Also note that echoj does not provide it's own newline sequence as output. We do this ourselves by placing the code for an ASCII linefeed ($0a) as the last string paramter. Try this example.
You should see:
You may wonder why we don't also specify a carriage-return ($0d) character with each linefeed. For Windows, most of the C libraries seem to translate the $0a character into a combination of $0d and $0a when going to the console or when a file is opened in text mode. The only situations where we really need to concern ourselves with the carriage-return character will be when we are using echoj to write to binary files and we need to issue a newline sequence.
The -f and -m options can be used to write to files as an option to using the standard command-processor's redirection symbols.
Consider the following example:
The above code writes the same sequence of lines from a prior example to the text file tmp.txt. The default file open mode that echoj uses is "w" for write new text file. This mode opens a file as output and deletes any existing contents if the file had already existed.
Let's append the same line to the file by specifying mode "a" ( append to text file ):
In the output file, you should now see:
Let's use the binary options to build a small .com file called alphabet.com. ( You can see the raw assembly source to this file in this blog's topmost masthead image. )
If you now execute alphabet.com you should see the alphabet on the console in upper-case.
These file open mode strings are passed in verbatim to the C fopen() function, so echoj will respond to modes such as "a+b" ( append binary ), but I've yet to really experiment with any options beyond the four listed in the syntax-help screen.
The source and executable file for echoj above can be downloaded in a single archive at: http://www.mailsend-online.com/wp/echoj.zip
Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless
Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.
Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks
Previous post: | Internet Protocols and Rhino JavaScript |
---|---|
Next post: | Command-Line Image Format Conversion |
Compiling C from the Command Line with Pelles C
A Forthcoming Marvel Movie Villain
Uninstalling Problematic Windows Software
A JavaScript REPL for Android Devices
Compiling Rhino JavaScript to Java
Command-Line Image Format Conversion
Send GMail From the Windows Command-Line with MailWrench
Preventing Windows Screen-Saver Activation
A Data Manipulation Library for TAP
BPL: Batch Programming Language Interpreter
An Interview with Tom Zimmer: Forth System Developer
Yet Another Config File Reader for Go (Golang)
Filling a Slice Using Command-line Flags in Go (Golang)
An RPN Interpreter in Go (Golang)
Simulating Try-Catch in Go (Golang)
Sending GMail with Go (Golang)
My First C64 Interrupt Program
Playing with OS/X Text to Speech
The Villain at the end of Marvel's Avengers Move is...
Processing GMail items with Ruby
Happy 30th Birthday, Commodore 64
MailWrench CSharp Command Line Mailer for Windows is now Free Software
Rainy Day Fun with the HTML DOM
Building a World War II Foxhole Radio
Prototerp – Unleashing a JavaScript REPL via the Mobile Browser