How do you use SETLOCAL in a batch file?

后端 未结 3 1283
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-16 09:44

How do you use setlocal in a batch file? I am just learning scripting and would like it explained to me in very simple terms.

I have a script that stops

3条回答
  •  不思量自难忘°
    2020-12-16 10:45

    Suppose this code:

    If "%getOption%" equ  "yes" (
       set /P option=Enter option: 
       echo Option read: %option%
    )
    

    Previous code will NOT work becase %option% value is replaced just one time when the IF command is parsed (before it is executed). You need to "delay" variable value expansion until SET /P command had modified variable value:

    setlocal EnableDelayedExpansion
    If "%getOption%" equ  "yes" (
       set /P option=Enter option: 
       echo Option read: !option!
    )
    

    Check this:

    set var=Before
    set var=After & echo Normal: %var%  Delayed: !var!
    

    Guess what the output is...

提交回复
热议问题