· batch-scripts

Spaces in batch scripts

Since reading The Pragmatic Programmer I’ve become a bit of an automation junkie and writing batch scripts falls right under that category.

Unfortunately, nearly every single time I write one I forget that Windows really hates it when you have spaces in variable assignments, and I forget how to print out a usage message if the right number of parameters are not passed in.

So as much for me as for everyone else, this is how you do it:

12345678910111213141516~
</td>
@ECHO offIF [%1]==[] GOTO usageIF [%2]==[] GOTO usageset VAR1=%1set VAR2=%2rem important client stuffgoto end:usageecho Usage: script.bat var1 var2:endecho Script finished~
</td>
</tr>
</tbody></table>

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket