MS-DOS parameterized batch script

Although not as powerful as a Bash scripts, yet it is possible to craft quite involved scripts in MS-DOS. This is especially important as, believe it or not 😉 most of the world PC’s are powered by some Windows version, so enter the underlying MS-DOS.

When your .bat file is distributed to a customer, you generally do not want him to modify it …. but very often, you need to allow some degree of customisation. As an Engineer, you would simply rely on environment variables being defined /modified, so your .bat can run on several context. However, it is very dangerous to give this possibility to somebody else for several reasons, among which

  • Syntax for defining/modifying the variables need to be taught. Remember the golden rule: the Customer is NOT a software guy at all.
  • Scope / Lifetime of environment variables has also to be taken into account
  • Everything has to stay simple (KISS)

The best solution I found is to define a simple configuration file for the script, as follows:

Let’s name our configuration file “site.conf”

REPAIR_SITE=PARIS
PARIS_IP=10.188.62.97
NICE_IP=176.16.5.15
BASEPRODUCT=AKILLERPRODUCT
TYPE=PROTO
IMAGE=software_image_version_1

Here we define several environment variables, all related to a maintenance location (REPAIR_SITE). So we define the local IP address, some product features, etc … basically whatever you feel the need.

The advantage of such a small configuration file is that, the syntax is VERY simple and manageable by the Customer (pure text, easy formatting).

Then your script (this one, unchanged), would be contain the following lines, typically at the very beginning

@echo OFF
REM Get local environment
for /f %%x in (site.conf) do set %%x

so each line of the config file is read, and converted into a proper “SET” command. Then, the rest of the processing can be executed, based on those variables.

Some improvements can of course be brought to this simple example, like undef’ing the variables before and after, but you got the idea.

Leave a Reply

Your email address will not be published.