Example of delayed expansion in batch file

前端 未结 3 451
长发绾君心
长发绾君心 2020-11-21 14:40

Can someone give me an example of where a batch script would act differently with or without delayed expansion? Are there any situations where you would NOT want to use del

3条回答
  •  悲哀的现实
    2020-11-21 15:24

    Max's answer gives an example of where a batch script would act differently with or without delayed expansion.

    For the sake of completeness, let's answer another part of the question and show a situation where you would NOT want to use delayed expansion when your data contain an exclamation mark ! (and show two ways of processing such data):

    @ECHO OFF
    SETLOCAL EnableExtensions DisableDelayedExpansion
    
      set "_auxFile=%temp%\%~n0.txt"
      rem create multiline sample file
      >"%_auxFile%" ( for /L %%G in (1,1,3) do echo line %%G is 100%% valid! Sure! Hurrah!)
      rem create one-line sample file
      >"%_auxFile%" echo this line is 100%% valid! Sure! Hurrah!
    
      echo(
      echo --- file content 
      type "%_auxFile%"
    
      echo(
      SETLOCAL EnableDelayedExpansion
        echo --- enabled delayed expansion chokes down unescaped exclamation marks^^^! "^!"
        for /F "usebackq delims=" %%G in ("%_auxFile%") do (
          set "_auxLine=%%~G"
          echo loop var=%%~G
          echo _auxLine=!_auxLine!
        )
      ENDLOCAL
      echo(
      SETLOCAL DisableDelayedExpansion
        echo --- toggled delayed expansion works although might be laborious!
        for /F "usebackq delims=" %%G in ("%_auxFile%") do (
          set "_auxLine=%%G"
          echo loop var=%%G
          SETLOCAL EnableDelayedExpansion
            echo _auxLine=!_auxLine!
          ENDLOCAL
        )
      ENDLOCAL
      echo(
      SETLOCAL DisableDelayedExpansion
        echo --- keep delayed expansion DISABLED: use CALL command!
        for /F "usebackq delims=" %%G in ("%_auxFile%") do (
          set "_auxLine=%%G"
          echo loop var=%%G
          call :ProcessVar
        )
      ENDLOCAL
    
      rem delete the sample file
      del "%_auxFile%"
    ENDLOCAL
    goto :eof
    
    :ProcessVar
      echo _auxLine=%_auxLine%
      echo WARNING: neither !_auxLine! nor %%G loop variable is available here!  
    goto :eof
    

    Note that above script shows proper ways of escaping

    • % percent sign by %% doubling it (delayed expansion does not matter), and
    • ! exclamation mark if delayed expansion is enabled:
      • "^!" if enclosed in a pair of double quotes, then use the cmd and batch-script general escape character ^ caret;
      • ^^^! otherwise, use three ^ carets.

    Output:

    ==> D:\bat\SO\10558316.bat
    
    --- file content
    this line is 100% valid! Sure! Hurrah!
    
    --- enabled delayed expansion chokes down unescaped exclamation marks! "!"
    loop var=this line is 100% valid Hurrah
    _auxLine=this line is 100% valid Hurrah
    
    --- toggled delayed expansion works although might be laborious!
    loop var=this line is 100% valid! Sure! Hurrah!
    _auxLine=this line is 100% valid! Sure! Hurrah!
    
    --- keep delayed expansion DISABLED: use CALL command!
    loop var=this line is 100% valid! Sure! Hurrah!
    _auxLine=this line is 100% valid! Sure! Hurrah!
    WARNING: !_auxLine! as well as %G loop variables are not available here!
    
    ==>
    

提交回复
热议问题