Pros and Cons of starting a script "setlocal"
Posted: 30 Jun 2010 15:12
I notice many solutions here commence with the line
@echo off &setlocal
In the past I have only ever used setlocal for the benefit of delayed expansion,
and normally I try to avoid delayed expansion where possible and never consider a setlocal.
I can see an advantage that any new environment variables will be purged from memory when the script ends, but I guess that ONLY applies if CMD.EXE is launched once and then a succession of batch commands are manually invoked - but how significant is that space now the cheapest computers have 1 or more GB of RAM, and we are no longer subject to Bill Gates original decree that 640 KB was more than enough RAM for anybody.
Are there any other benefits to always starting with SETLOCAL ?
One disadvantage that occurs to me is that when I write I script I always test it,
and then I need to fix it.
Often when the script gives the wrong answer, I find the clue with
SET
which shows all the variables, and what I did wrong to the new variables,
and that debug aid will vanish if my new variables evaporate upon an (implied) endlocal.
Regards
Alan
P.S. With only myself + 4 guests logged in, this site is incredibly slow.
Is it under another spammer/hacker attack ?
@echo off &setlocal
In the past I have only ever used setlocal for the benefit of delayed expansion,
and normally I try to avoid delayed expansion where possible and never consider a setlocal.
I can see an advantage that any new environment variables will be purged from memory when the script ends, but I guess that ONLY applies if CMD.EXE is launched once and then a succession of batch commands are manually invoked - but how significant is that space now the cheapest computers have 1 or more GB of RAM, and we are no longer subject to Bill Gates original decree that 640 KB was more than enough RAM for anybody.
Are there any other benefits to always starting with SETLOCAL ?
One disadvantage that occurs to me is that when I write I script I always test it,
and then I need to fix it.
Often when the script gives the wrong answer, I find the clue with
SET
which shows all the variables, and what I did wrong to the new variables,
and that debug aid will vanish if my new variables evaporate upon an (implied) endlocal.
Regards
Alan
P.S. With only myself + 4 guests logged in, this site is incredibly slow.
Is it under another spammer/hacker attack ?