Trying to understand this code (Batch, CMD)

    @echo off
setlocal enabledelayedexpansion
::just a sample adapter here:
set "adapter=Ethernet adapter Local Area Connection"
set adapterfound=false
echo Network Connection Test
for /f "usebackq tokens=1-2 delims=:" %%f in (`ipconfig /all`) do 
(
    set "item=%%f"
    if /i "!item!"=="!adapter!" 
  (
        set adapterfound=true
      )
   else if not "!item!"=="!item:IP Address=!" if "!adapterfound!"=="true" 
  (
        echo Your IP Address is: %%g
        set adapterfound=false
      )
)

VERY new to the party, can someone explain a few things here:

  • What does setlocal enabledelayedexpansion do in this particular case?
  • What does usebackq do? (I tried to see this, but did not quite understand)
  • How was the variable %% g initialized and global or local?

Thank you for your time!

+4
source share
1 answer
  • Deferred expansion will cause the variables to expand at run time, rather than during parsing.
    The code shown below will be displayed second second, but prints instead first second.

     setlocal EnableDelayedExpansion
     set var=first
     set var=second & Echo %var% !var!
    

    Source: SS64

  • usebackq backquotes (`dir`) for , . , .

    echo Documents:
    for /f "usebackq" %%i in (`dir /b "C:\Users\%username%\Documents\"`) do (
      echo * %%i
    )
    pause
    
  • . , %%i for, .

+2

Source: https://habr.com/ru/post/1662879/


All Articles