2011-06-27

Windows and the command line

If I remember correctly I was about 14 when I wrote my first MS DOS batch file. I can't say that I write many of them but from time to time I think a VB script is too much for just some little work.

On the other hand I began to really learn Linux shell scripting only about 6 months ago. After that I find the Windows batch file language a very poor thing. It has a poor feature set, a lousy documentation and it is buggy. And this still applies for Windows 2008r2 - so I am talking about the newest OS version.

Right now I lost about two hours investigating a problem to find out that the batch file I wrote, did not work. The problem was caused at a completely unexpected place where I had a few lines that I previously tested separately - on the command line.

Guess what: Behaviour may be different if I run a command on the commandline or from within a batch file!

Here is an example - try this on the command line:
set var1=fluffi
echo _%var1%_ _%var2%_
The result will be: _fluffi_ _%var2%_

And now put those two lines into a .bat or .cmd file and run it.
The result is: _fluffi_ __

A pretty difference, isn't it!

In my case I was passing a string like this "blablabla%muh%bla" as parameter to a called program and the parameter did not fully arrive in the batch file while it did when I tried on the commandline.

What's that?!?! - Is this the Microsoft logic? - I think so... one only has to look at the evolution of MS Word...

Oh - I guess you want a solution: In the batch file you have to escape % doubling it - %%.

Related Post: The Microsoft Logic