How to use special characters like letters in the batch files

For most of the users, the batch files are just a short script files for the automation of tasks. However, we can use those files for more than just running some program with additional parameters.

A few months ago I made a filter files for the XMail server using the batch files to process incoming e-mail message and conduct appropriate action after the test. One of the filters is the filter for non-existing mailboxes.

The employees will come and go from the company. Doing their job they will make some business contacts or subscribe to mailing lists. After they leave a company, someone who has their old contact may try to send them an e-mail.

Depending on the company policy such e-mail may be redirected to other employee, captured in a catch-all account or rejected and returned to the sender. If we want to return an e-mail, we must specify the reason why it is rejected from our e-mail system.

Usual the way to do that is to send back status message with the code 550 or 5.5.0 and short description in the single line of a text. Most common text is “The mailbox <username> does not exists”.

 

Creating dynamic answer

My batch file based filter dynamically creates the answer file with such message. The part “username” should be replaced with the recipient account name.

This task doesn’t sound too heavy. We can use the batch command echo and the output redirection. Instead on the screen, the output of the command will be sent (redirected) in the text based file on the disk.

2016-06-21 11_08_29-escaping.bat - Notepad

We will try with the following command in the batch file:

echo 550 The mailbox <%1> does not exists >> %1.rej

However, this command will be executed in a completely different way.

2016-06-21 11_09_28-C__Windows_System32_cmd.exe

The batch command processor will treat characters < and > as redirection commands. This means that we need to tell the processor that those characters should be treated as the text.

 

Escaping the redirection characters

We can even use such special characters, like <, > or even |, as the regular text. To do so, we must use another text character before them. If we put the caret sign (^) before any of those commands, they will be treated as plain text.

Now we will edit our command to change special characters to the plain text signs.

echo 550 The mailbox ^<%1^> does not exists >> %1.rej

After this change our batch file will process all rejection messages correctly. The sender will receive a response like

550 The mailbox <JohnSmith@company.com> does not exists
2016-06-21 11_14_41-C__Windows_System32_cmd.exe

 

Improved output with the special characters

As we have already seen in the previous example, we can use the redirection signs as the brackets. Moreover, we can use other characters, like the pipe sign (|), in the output text. Many of those characters are commonly used in the everyday writing.

2016-06-21 16_19_01-C__Windows_System32_cmd.exe

The possibility to use them inside the output text opens possibilities to enrich the user experience. We can then display text on the screen or insert it in the file in a way that is much more readable; already we mentioned the pipe sign, which can (for example) be used to draw a column delimiting line in the table.

You can experiment with different outputs to find the one that will fit the best. Remember that the batch file has just eight commands that can do the magic.

May the batch file’s magic be with you! 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s