Should I always enable delayed expansion for a whole batch script?

Viewed 217

Should I always put setlocal enableDelayedExpansion at the start a batch script, even when I don't need it?

Does it have any impact on performance, or will it cause any problem?

1 Answers

I enable delayed expansion nearly always at the beginning of any batch file.

Because it has two main advantages:

  • Expansion of !varname! is evaluated when the expression is executed, not when it is parsed, like percent expansion, this avoid many problems
  • The expanded content is always safe and will not be (re)parsed further, special characters doesn't harm
  • The setlocal prevents the environment of polution with local variables

The only drawback is the handling of exclamation marks itself.
But even that can be solved with some glue code

Related