Fun with Batch files



It goes like this: create a small script that will take the files from a folder (only the top folder, not the sub directories) and will copy them to Program Files (32 bit). How hard can it be?

Well… If you have a programming background and you like to format the code then, such a task takes 1 hour, otherwise… 5 minutes.

So, I wrote a batch file (just part of it displayed here):

SET /P config=Configuration to deploy (1 = Debug; 2 = Release):
ECHO Setting up folders...
SET instpath = %ProgramFiles(x86)%
IF %config% == 1 (set drop = source\bin\x86\Debug)
IF %config% == 2 (set drop = bin\x86\Release)

Which is incorrect. Can you spot the mistake?

I will highlight one of the lines for you, maybe you can spot it then.

SET /P config=Configuration to deploy (1 = Debug; 2 = Release):
ECHO Setting up folders...
SET instpath = %ProgramFiles(x86)%
IF %config% == 1 (set drop = source\bin\x86\Debug)
IF %config% == 2 (set drop = bin\x86\Release)

Another hint: lines 5, 6 and 7 are incorrect.

Let me fix it and maybe you’ll see the problem.

SET /P config=Configuration to deploy (1 = Debug; 2 = Release):
ECHO Setting up folders...
SET instpath=%ProgramFiles(x86)%
IF %config% == 1 (set drop=source\bin\x86\Debug)
IF %config% == 2 (set drop=bin\x86\Release)

Can you see the difference now? Some of you might not…

In BAT files SET instpath=%ProgramFiles(x86)% and SET instpath = %ProgramFiles(x86)% are not the same. The first one will set the variable, while the second one will fail (without warning/error). Apparently, there should be no space between the variable name and the equal sign when setting a value.