Script ignoring the exit command and exiting the BATCH / CMD function

This script that I wrote should parse and extract IP information from WMIC. The problem is that when it is used on a computer with only one DNS server input, the script fails. It seems that it runs the function: remove, even if the DNS2 variable is not set, and then: the remove function does not work, I am not familiar and ignore the exit command and breaks out of the function and in the script part I do not want this. Here is the violation code, check it by installing your DNS servers and index. The script works with two DNS servers and does not have only 1.

@echo off
setlocal enabledelayedexpansion
SETLOCAL ENABLEEXTENSIONS
wmic NICCONFIG where "Index = 1" get /Value | more > storage.txt

for /f "tokens=1-9 delims=,}{=" %%f in (storage.txt) do (
    set cat=%%f
    IF /i !cat!==IPAddress (set IP=%%g)
    IF /i !cat!==IPSubnet (set SUB=%%g)
    IF /i !cat!==DefaultIPGateway (set GW=%%g)
    IF /i !cat!==DNSServerSearchOrder (
        set DNS=%%g
        set DNS2=%%h
    )
    IF /i !cat!==IPEnabled (set enabled=%%g)
    IF /i !cat!==Description (set desc=%%g)
)
if /i %enabled%==True (
if defined IP (
    set item=IP
    CALL :remove
        )
if defined SUB (
    set item=SUB
    CALL :remove
        )
if defined GW (
    set item=GW
    CALL :remove
        )
if defined DNS (
    set item=DNS
    CALL :remove
        )
if defined DNS2 (
    set item=DNS2
    CALL :remove
        )
)
goto :menu
echo SHOULDN'T BE HERE
pause
:::::::::Function to remove quotes::::::::::::::
:remove
for /f tokens^=1^ delims^=^" %%p in (!%item%!) do (
    set !item!=%%p
    rem echo !%item%!
    EXIT /b
    )
echo I BROKE OUT OF THE FUNCTION
pause
:menu
echo I worked!
echo %IP%
echo %SUB%
echo %GW%
echo %DNS1%
if defined DNS2 ( echo %DNS2% )
pause

Here is another example of using LotPings refinement, note that even though there is no DNS2, it still does the bottom "if defined"

@echo off
SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion
Set Prop=IPAddress IPSubnet DefaultIPGateway DNSServerSearchOrder IPEnabled Description
for /f "tokens=1-9 delims=,}{=" %%f in (
  'wmic NICCONFIG where "Index = 1" get /Value^|findstr /i /B "%Prop%"'
) do (IF /i %%f==IPAddress set "IP=%%~g"
      IF /i %%f==IPSubnet  set "SUB=%%~g"
      IF /i %%f==DefaultIPGateway  set "GW=%%~g"
      IF /i %%f==DNSServerSearchOrder (
        set "DNS=%%~g"
        set "DNS2=%%~h"
      )
      IF /i %%f==IPEnabled   set "enabled=%%~g"
      IF /i %%f==Description set "desc=%%~g"
)
set|Findstr /i /B "IP SUB GW DNS Ena Desc"
echo I worked!
echo %DNS%
echo %DNS2%
if defined DNS2 ( echo DNS2 found, shouldn't have found this)
pause
+4
3

EDIT :

@echo off
SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion
Set Prop=IPAddress IPSubnet DefaultIPGateway DNSServerSearchOrder IPEnabled Description
for /f "tokens=1-9 delims=,}{=" %%f in (
  'wmic NICCONFIG where "Index = 1" get /Value^|findstr /i /B "%Prop%"'
) do (IF /i %%f==IPAddress set "IP=%%~g"
      IF /i %%f==IPSubnet  set "SUB=%%~g"
      IF /i %%f==DefaultIPGateway  set "GW=%%~g"
      IF /i %%f==DNSServerSearchOrder (
        set "DNS=%%~g"
        set "DNS2=%%~h"
      )
      IF /i %%f==IPEnabled   set "enabled=%%~g"
      IF /i %%f==Description set "desc=%%~g"
)
If "%DNS2:~3%" EQU "" Set "DNS2="
set|Findstr /i /B "IP SUB GW DNS Ena Desc"
echo I worked!
pause
+3

, .
-

IF /i %%f==DNSServerSearchOrder (
        set "DNS=%%~g"
        set "DNS2=%%~h"
        echo #1 "%%~g"
        echo #2 "%%~h"
        if defined dns echo DNS defined
        if defined dns2 echo DNS2 defined
      )

-

> #1 "10.51.16.9"
> "2 "
> DNS defined
> DNS2 defined

, DNS2 , echo #2 "%%~h" !
, wmic \r\r\n \r\n.
DNS2 \r.

sanitize, \r .
, \r , ( , %%p)

:sanitize
set "var=!%1!"
set "%1=%var%"
exit /b

IF /i %%f==DNSServerSearchOrder (
        set "DNS=%%~g"
        set "DNS2=%%~h"
        call :sanitize DNS
        call :sanitize DNS2
        echo #1 "!DNS!"
        echo #2 "!DNS2!"
        if defined dns echo DNS defined
        if defined dns2 echo DNS2 defined
      )

...

0

for /F , wmic:

rem // Code before the loop
for /F "delims=" %%e in ('
    wmic NICCONFIG where "Index = 1" get /Value ^| findstr /I /B "%Prop%"
') do (
    for /F "tokens=1-9 delims=,}{=" %%f in ("%%e") do (
        rem // Code inside of the loop...
    )
)
rem // Code after the loop...

wmic Unicode-, ANSI for /F, , . for /F .

0

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


All Articles