Originally published on: Sun, 06 Sep 2009 16:12:42 +0000
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
Save to del.icio.us
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 |
Subscribe!
Auto Save Clipboard Images Redux
Extending SpiderMonkey JavaScript on Windows
Rhino JavaScript to EXE with launch4j
Compiling Rhino JavaScript to Java
Directory Traversal in Rhino JavaScript
A Command-Line MP3 Player for Windows
Auto Save Images from the Clipboard
Java in a Windows EXE with launch4j
An Interview with Tom Zimmer: Forth System Developer
Setting Windows Console Text Colors in C
Understanding TRS-80 CMD Files
TAP : A Command Processor Library
Speeding up JRuby with NailGun
Tracing XSLT with a Tiny Java Web Server
An Interview with the Author of the French Silk Assembler
MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer